存储过程、插入数据后直接过去主键id

DECLARE @sql nvarchar(4000)
DECLARE @cou int
SET @sql='INSERT INTO people values('''+'xiaohong'+''');select @temp=@@IDENTITY' 
EXEC sp_executesql @sql, N'@temp int out', @cou OUT
INSERT INTO child VALUES(@cou, '小兰')
CREATE PROC chjj @name nvarchar(1000)
AS
BEGIN
  DECLARE @sql nvarchar(4000)
  DECLARE @cou int
  SET @sql='INSERT INTO people values('''+@name+''');select @temp=@@IDENTITY' 
  EXEC sp_executesql @sql, N'@temp int out', @cou OUT
  IF @cou > 85
BEGIN
INSERT INTO child VALUES(@cou, '小兰') END
END 调用存储过程:EXEC dbo.chjj '李厚鑫s'

 

上一篇:c – ATL / WTL是否仍然需要使用全局_Module变量?


下一篇:SQL-sp_executesql