gpt4 book ai didi

sql - 在sql server中执行一个sql字符串

转载 作者:行者123 更新时间:2023-12-02 04:22:43 25 4
gpt4 key购买 nike

我的代码如下,不知为何总是在@Name

附近出错
DECLARE @Name nvarchar(MAX)  =  '(mm.dll, ben and jerry.exe)'
DECLARE @sql nvarchar(MAX)= 'SELECT OrderName,
customer.version,
count(DISTINCT company.CID) as Counts
FROM [CompanyData] company
INNER JOIN [vendor] mav on company.CID = mav.CID
LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId AND company.DId = customer.DId
WHERE OrderName in' + @Name+ '
GROUP BY
customer.version, OrderName'

EXEC sp_executesql @sql

最佳答案

在你的 @Name 声明中放置一个单引号,然后只删除其中的散列 (#):

DECLARE @Name nvarchar(MAX)='(''mm.dll'', ''ben and jerry.exe'')'
DECLARE @sql nvarchar(MAX)=
'SELECT
OrderName,
customer.version,
count(DISTINCT company.CID) as Counts
FROM [CompanyData] company
INNER JOIN [vendor] mav on company.CID = mav.CID
LEFT OUTER JOIN [Customer] customer on company.VendorId = customer.VendorId
AND company.DId = customer.DId
WHERE OrderName in ' + @Name+ '
GROUP BY customer.version, OrderName'

EXEC sp_executesql @sql

关于sql - 在sql server中执行一个sql字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179585/

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