在执行执行存储过程的动态SQL时,我正在努力从序列中获取值。我甚至可以这样做吗?
我正在使用SQL Server 2014.这是我的错误:
Msg 102,Level 15,State 1,Line 116
'NEXT'附近的语法不正确。
我的代码:
Declare @Script1 NVARCHAR(max);
Declare @ScriptSP1 NVARCHAR(max);
Declare @ScriptSeq1 NVARCHAR(max);
Declare @ScriptExecSP1 NVARCHAR(max);
Set @Script1 = N'Create table dbo.tblParentSponsor(
[ParentSponsorId] INT PRIMARY KEY,
[Name] NVARCHAR(50) NOT NULL,
[Surname] NVARCHAR(50) NOT NULL,
[Cellphone_Number] CHAR(10) NOT NULL,
[Telephone_Number] CHAR(10) NOT NULL,
[Address] NVARCHAR(50) NOT NULL,
[Region] NVARCHAR(50) NOT NULL,
[Email] NVARCHAR(254) NOT NULL,
[ID_Number] CHAR(13) NOT NULL
);'
EXEC sp_executesql @Script1;
Set @ScriptSP1 = N'CREATE PROCEDURE dbo.[sp_Insert_Into_tblParentSponsor]
@Id INT,
@Name NVARCHAR(50),
@Surname NVARCHAR(50),
@Cellphone_Number CHAR(10),
@Telephone_Number CHAR(10),
@Address NVARCHAR(50),
@Region NVARCHAR(50),
@Email NVARCHAR(254),
@ID_Number CHAR(13)
AS
BEGIN
INSERT INTO dbo.tblParentSponsor(ParentSponsorId,Name,Surname,Cellphone_Number,Telephone_Number,Address,Region,Email,ID_Number)
VALUES(@Id,@Name,@Surname,@Cellphone_Number,@Telephone_Number,@Address,@Region,@Email,@ID_Number)
END'
EXEC sp_executesql @ScriptSP1;
SET @ScriptSeq1 = 'CREATE SEQUENCE dbo.TestS
AS [int]
START WITH 1
INCREMENT BY 1'
EXEC sp_executesql @ScriptSeq1;
SET @ScriptExecSP1 = 'dbo.[sp_Insert_Into_tblParentSponsor] NEXT VALUE FOR dbo.TestS,S,S,1,1,1,1,1,1;'
EXEC sp_executesql @ScriptExecSP1;
答案 0 :(得分:0)
也许这对你有用吗?
DECLARE @nextValue INT;
SET @nextValue = NEXT VALUE FOR dbo.TestS
SET @ScriptExecSP1 = 'dbo.[sp_Insert_Into_tblParentSponsor] ' +
CAST( @nextValue AS VARCHAR(10) ) +
',''S'',''S'',''1'',''1'',''1'',''1'',''1'',''1'';'
EXEC sp_executesql @ScriptExecSP1;
(使用你希望将@nextValue转换为字符串的任何转换或转换方法。)