如何使用T-SQL执行保存在表中的SQL语句

时间:2013-07-27 19:59:13

标签: sql sql-server tsql

是否可以使用T-SQL执行存储在表中的SQL语句?

DECLARE @Query text
SET @Query = (Select Query FROM SCM.dbo.CustomQuery)

表中存储的语句是ad-hoc语句,可以 SELECT TOP 100 * FROM ATable 到更复杂的语句:

Select
J.JobName As Job,
JD.JobDetailJobStart AS StartDate,
JD.JobDetailJobEnd AS EndDate,
(
    SELECT (DATEDIFF(dd, JD.JobDetailJobStart, JD.JobDetailJobEnd) + 1) -(DATEDIFF(wk, JD.JobDetailJobStart, JD.JobDetailJobEnd) * 2) -(CASE WHEN DATENAME(dw, JD.JobDetailJobStart) = 'Sunday' THEN -1 ELSE 0 END) -(CASE WHEN DATENAME(dw, JD.JobDetailJobEnd) = 'Saturday' THEN -1 ELSE 0 END)
) AS NumberOfWorkingDays,
JD.JobDetailDailyTarget AS DailyTarget,
JD.JobDetailWeeklyTarget AS WeeklyTarget,
JD.JobDetailRequiredQTY AS RequiredQuantity,
(
    Select SUM(sJL.JobLabourQuantityEmployees) From JobLabour sJL
) AS NumberOfEmployees,
(
    Select 
        SUM((sEM.EmployeeDesignationDefaultRate * sJL.JobLabourQuantityEmployees)*8)*(SELECT (DATEDIFF(dd, JD.JobDetailJobStart, JD.JobDetailJobEnd) + 1) -(DATEDIFF(wk, JD.JobDetailJobStart, JD.JobDetailJobEnd) * 2) -(CASE WHEN DATENAME(dw, JD.JobDetailJobStart) = 'Sunday' THEN -1 ELSE 0 END) -(CASE WHEN DATENAME(dw, JD.JobDetailJobEnd) = 'Saturday' THEN -1 ELSE 0 END))
    from EmployeeDesignation sEM
    Inner join JobLabour sJL on sJL.EmployeeDesignationID = sEM.EmployeeDesignationID
) AS FullEmployeeRate



from Job J
Inner Join JobDetail JD on JD.JobID = J.JobID
Inner Join JobLabour JL on JL.JobID = J.JobID

WHERE J.JobActive = 0

我想执行我从T-SQL声明的 @Query 变量。这可能吗? (我正在运行MSSQL 2005环境)

4 个答案:

答案 0 :(得分:13)

您可以使用

EXECUTE sp_executesql @Query

运行你的T-SQL

这是指向SQL Server 2005的MS docn的链接

http://msdn.microsoft.com/en-us/library/ms188001%28v=sql.90%29.aspx

答案 1 :(得分:9)

上一个答案允许您运行一个语句,并且有效。问题是如何运行存储在表中的SQL语句,我将其作为多个语句执行。对于这个额外的步骤,有一个while循环涉及迭代需要运行的每个语句。

-- Author: Chad Slagle 
DECLARE @Table table (RID BIGINT IDENTITY(1,1) PRIMARY KEY CLUSTERED, 
                        SQLText NVARCHAR(MAX) )

DECLARE  @StatementMax INT 
        ,@statementMin INT
        ,@isTest TINYINT = 1
        ,@SQLStatement NVARCHAR(MAX)
-- Insert SQL Into Temp Table
INSERT INTO @table (SQLText)
VALUES ('SELECT @@Version');
INSERT INTO @table (SQLText)
VALUES ('SELECT SERVERPROPERTY(''ProductVersion'')')
-- Get your Iterator Values
SELECT @statementMAX = MAX(RID), @statementMIN = MIN(RID)  FROM @table

IF @isTest = 1 BEGIN SELECT *, @statementMax AS MaxVal, @StatementMin AS MinVal FROM @Table END
-- Start the Loop
WHILE @StatementMax >= @statementMin
BEGIN
    SELECT @SQLStatement = SQLText FROM @table WHERE RID = @statementMin        -- Get the SQL from the table 

    IF @isTest = 1 BEGIN SELECT 'I am executing: ' + @SQLStatement AS theSqlBeingRun, GETDATE(), @statementMin, @StatementMax END  
    ELSE 
    BEGIN 
        EXECUTE sp_ExecuteSQL @SQLStatement                 -- Execute the SQL 
    END
        DELETE FROM @table WHERE RID = @statementMin        -- Delete the statement just run from the table
        SELECT @statementMIN = MIN(RID)  FROM @Table        -- Update to the next RID
    IF @isTest = 1 BEGIN  SELECT * FROM @table END

END

在Summary中,我创建了一个临时表并在其中放入了一些SQL,使用IDENTITY(RID)字段为while循环提供迭代器。然后运行while循环。在该示例中,您应该返回SQL版本的两个视图。我在2k8上建造了这个,我希望有一天它可以帮助别人堵塞......

答案 2 :(得分:1)

我们使用更简单的方法。将脚本(原始sql或存储过程调用)存储在一个表中,该表包含一个包含所述脚本的标识代码的列。在脚本中使用占位符来获取参数。任何经常使用的脚本都可以在您的应用或Web配置文件中“键入”。如果脚本需要按特定顺序执行,请在表中放置一个序数列。然后可以将实际的“脚本”拉入c#列表或数组,传递给数据库类库并相应地执行。这使您可以动态控制SQL,并允许您在数据库端对所述脚本进行更改,而无需重新编译主应用程序。

答案 3 :(得分:0)

TRUNCATE  TABLE AllTableUnion
DECLARE @Query2 Nvarchar(MAX)
SET @Query2='SELECT * FROM #UnionString t1)A'
INSERT INTO AllTableUnion 
EXEC(@Query2)
DECLARE @Query4 Nvarchar(MAX)=(SELECT Query  FROM AllTableUnion)
EXECUTE sp_ExecuteSQL @Query4