gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:12:30 26 4
gpt4 key购买 nike

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

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

存储在表中的语句是临时语句,可以是 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环境)

最佳答案

您可以使用

EXECUTE sp_executesql @Query

运行 T-SQL

这是 SQL Server 2005 的 MS 文档的链接

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

关于sql - 如何使用 T-SQL 执行保存在表中的 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com