使用动态 SQL - 自定义通用多操作存储过程(带 TVPar)
using Dynamic SQL - Custom Universal Multi-Action Stored Procedure (with TVPar)
这是我开始写的存储过程
问题是我不知道如何处理从 TV par 读取值的部分:
Must declare the table variable "@TestMultiActionViaRowIndexTVPar".
ALTER Proc [dbo].[MultiActionViaRowIndexSpTVP]
@SelectedSDTOName varchar (50), @SelectedAction varchar(10), @TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY
as BEGIN
declare @CmdStr varchar(500) = '';
if(@SelectedAction = 'SELECT') Begin --test for one of the "Action" types
SET @CmdStr = 'SELECT * FROM ' + @SelectedSDTOName +
' WHERE RowIndex in (SELECT RowIndex FROM @TestMultiActionViaRowIndexTVPar)'
End
--else - other Action - @CmdStr will be according to action...
--finally execute constructed Cmdstr
Exec(@CmdStr);
END
使用 sp_executesql 将 TVP 或任何其他参数传递给参数化动态 SQL:
ALTER PROC [dbo].[MultiActionViaRowIndexSpTVP]
@SelectedSDTOName varchar (50)
, @SelectedAction varchar(10)
, @TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY
AS
DECLARE @CmdStr nvarchar(MAX);
IF @SelectedAction = 'SELECT'
BEGIN
SET @CmdStr = 'SELECT * FROM ' + @SelectedSDTOName +
' WHERE RowIndex in (SELECT RowIndex FROM @TestMultiActionViaRowIndexTVPar);'
END;
EXEC sp_executesql
@CmdStr
,N'@TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY'
,@TestMultiActionViaRowIndexTVPar = @TestMultiActionViaRowIndexTVPar;
GO
这是我开始写的存储过程 问题是我不知道如何处理从 TV par 读取值的部分:
Must declare the table variable "@TestMultiActionViaRowIndexTVPar".
ALTER Proc [dbo].[MultiActionViaRowIndexSpTVP]
@SelectedSDTOName varchar (50), @SelectedAction varchar(10), @TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY
as BEGIN
declare @CmdStr varchar(500) = '';
if(@SelectedAction = 'SELECT') Begin --test for one of the "Action" types
SET @CmdStr = 'SELECT * FROM ' + @SelectedSDTOName +
' WHERE RowIndex in (SELECT RowIndex FROM @TestMultiActionViaRowIndexTVPar)'
End
--else - other Action - @CmdStr will be according to action...
--finally execute constructed Cmdstr
Exec(@CmdStr);
END
使用 sp_executesql 将 TVP 或任何其他参数传递给参数化动态 SQL:
ALTER PROC [dbo].[MultiActionViaRowIndexSpTVP]
@SelectedSDTOName varchar (50)
, @SelectedAction varchar(10)
, @TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY
AS
DECLARE @CmdStr nvarchar(MAX);
IF @SelectedAction = 'SELECT'
BEGIN
SET @CmdStr = 'SELECT * FROM ' + @SelectedSDTOName +
' WHERE RowIndex in (SELECT RowIndex FROM @TestMultiActionViaRowIndexTVPar);'
END;
EXEC sp_executesql
@CmdStr
,N'@TestMultiActionViaRowIndexTVPar dbo.TestMultiActionViaRowIndexTVType READONLY'
,@TestMultiActionViaRowIndexTVPar = @TestMultiActionViaRowIndexTVPar;
GO