if object_id('ta')is not null drop table ta
create table ta(xh varchar(12),xm varchar(12),class varchar(12),cj int)insert ta select
'101','张三' , '1 年级',89 union all select
'102','利益', '1 年级',92 union all select
'103','张的胡','1 年级',81 union all select
'104','销售', '1 年级',70 union all select
'201','董事', '2 年级',99 union all select
'202','化学', '2 年级',88 union all select
'203','阿克', '2 年级',88 union all select
'204','第三方','2 年级',89 union all select
'301','张三', '3 年级',80 union all select
'302','张三', '3 年级',81 union all select
'303','张三', '3 年级',96
SET IDENTITY_INSERT dbo.ta ON --加这个好像也没用;with szy as
(select *,pm=row_number()over(partition by class order by cj)
from ta
)select * from ta
--消息 8101,级别 16,状态 1,第 4 行
--仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'ta'中的标识列指定显式值。
--不知道啥意思???
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))2.尝试在表中做以下操作:
INSERT INTO products (id, product) VALUES(3, 'garden shovel')结果会导致错误:“当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'products' 中的标识列插入显式值。”3.改用:
SET IDENTITY_INSERT products ON
INSERT INTO products (id, product) VALUES(1, 'garden shovel')返回正确。4.建立另外一个表products2,尝试相同插入操作:
CREATE TABLE products2 (id int IDENTITY PRIMARY KEY, product varchar(40))然后执行:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) VALUES(1, 'garden shovel')导致错误:“表 'material.dbo.products' 的 IDENTITY_INSERT 已经为 ON。无法对表 'products2' 执行 SET 操作。”改为执行:
SET IDENTITY_INSERT products OFF
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) VALUES(2, 'garden shovel')执行通过。5.尝试以下操作:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 SELECT * FROM products导致错误:“仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'products2' 中为标识列指定显式值。”6.改为:
SET IDENTITY_INSERT products2 ON
INSERT INTO products2 (id, product) SELECT * FROM products执行通过。总结:1.每一次连接会话中的任一时刻,只能对一个表设置IDENTITY_INSERT ON,且设置只对当前会话有效;
2.在对标识列执行插入操作进,一定要列出此标识列(当然,同时也就需要列出相关的其他列了)。附:
SQL Server帮助文档相关内容SET IDENTITY_INSERT
允许将显式值插入表的标识列中。语法
SET IDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OFF }参数
database是指定的表所驻留的数据库名称。owner是表所有者的名称。table是含有标识列的表名。注释
任何时候,会话中只有一个表的 IDENTITY_INSERT 属性可以设置为 ON。如果某个表已将此属性设置为 ON,并且为另一个表发出了 SET IDENTITY_INSERT ON 语句,则 Microsoft® SQL Server™ 返回一个错误信息,指出 SET IDENTITY_INSERT 已设置为 ON 并报告此属性已设置为 ON 的表。如果插入值大于表的当前标识值,则 SQL Server 自动将新插入值作为当前标识值使用。SET IDENTITY_INSERT 的设置是在执行或运行时设置,而不是在分析时设置。权限
执行权限默认授予 sysadmin 固定服务器角色和 db_owner 及 db_ddladmin 固定数据库角色以及对象所有者。示例
下例创建一个含有标识列的表,并显示如何使用 SET IDENTITY_INSERT 设置填充由 DELETE 语句导致的标识值中的空隙。-- Create products table.
CREATE TABLE products (id int IDENTITY PRIMARY KEY, product varchar(40))
GO
-- Inserting values into products table.
INSERT INTO products (product) VALUES ('screwdriver')
INSERT INTO products (product) VALUES ('hammer')
INSERT INTO products (product) VALUES ('saw')
INSERT INTO products (product) VALUES ('shovel')
GO-- Create a gap in the identity values.
DELETE products
WHERE product = 'saw'
GOSELECT *
FROM products
GO-- Attempt to insert an explicit ID value of 3;
-- should return a warning.
INSERT INTO products (id, product) VALUES(3, 'garden shovel')
GO
-- SET IDENTITY_INSERT to ON.
SET IDENTITY_INSERT products ON
GO-- Attempt to insert an explicit ID value of 3
INSERT INTO products (id, product) VALUES(3, 'garden shovel').
GOSELECT *
FROM products
GO
-- Drop products table.
DROP TABLE products
GO
是这个问题。好了那我这句没执行啊,没用啊?
if object_id('ta')is not null drop table ta
(select *,pm=row_number()over(partition by class order by cj)
from ta
)
这句是不是
(select *,pm=row_number()over(partition by class order by cj)
from ta
) as szy的意思?
FROM sysobjects
WHERE name = N'<table_name, sysname, test_table>'
AND type = 'U')
DROP TABLE <table_name, sysname, test_table>
GO
好像是这样的格式
不清楚了
如果前一次执行报错。那本次肯定有问题,都要先drop一下才行,不管是table,function,proc
不知道是什么道理?
但是后面没看到有用过它呢?
后面是我写错了。应该是
select * from szy where pm<=2
go
create table ta(xh varchar(12),xm varchar(12),class varchar(12),cj int)insert ta select
'101','张三' , '1 年级',89 union all select
'102','利益', '1 年级',92 union all select
'103','张的胡','1 年级',81 union all select
'104','销售', '1 年级',70 union all select
'201','董事', '2 年级',99 union all select
'202','化学', '2 年级',88 union all select
'203','阿克', '2 年级',88 union all select
'204','第三方','2 年级',89 union all select
'301','张三', '3 年级',80 union all select
'302','张三', '3 年级',81 union all select
'303','张三', '3 年级',96
--SET IDENTITY_INSERT dbo.ta ON --加这个好像也没用;with szy as
(select *,pm=row_number()over(partition by class order by cj)
from ta
)select * from szy
/*xh xm class cj pm
------------ ------------ ------------ ----------- --------------------
104 销售 1 年级 70 1
103 张的胡 1 年级 81 2
101 张三 1 年级 89 3
102 利益 1 年级 92 4
202 化学 2 年级 88 1
203 阿克 2 年级 88 2
204 第三方 2 年级 89 3
201 董事 2 年级 99 4
301 张三 3 年级 80 1
302 张三 3 年级 81 2
303 张三 3 年级 96 3
*/