Sql server sp_executesql或不带变量执行
我想在sql server 2012上执行此查询,而不使用变量:Sql server sp_executesql或不带变量执行,sql-server,tsql,variables,execute,sp-executesql,Sql Server,Tsql,Variables,Execute,Sp Executesql,我想在sql server 2012上执行此查询,而不使用变量: DECLARE @query VARCHAR(4000) set @query= concat('select * from customer where id in (', (select id from customer where comid=1 and code='30.00.0000'), ') order by code') execute @query 所以我试了一下: sp_executesql N' sel
DECLARE @query VARCHAR(4000)
set @query= concat('select * from customer where id in (',
(select id from customer where comid=1 and code='30.00.0000'),
') order by code')
execute @query
所以我试了一下:
sp_executesql N'
select concat(''select * from customer where id in ('',
(select id from customer where comid=1 and code=''30.00.0000''),
'') order by code'')'
没有效果,因为它生成查询而不是返回值
上面的版本是裁剪的。这是整个剧本:
DECLARE @query VARCHAR(4000)`
DECLARE @years VARCHAR(2000)`
SELECT @years = STUFF((
SELECT DISTINCT
'],[' + ltrim(str(etos))
FROM
(
select c.code , year(f.ftrdate) as etos , sum((it.outputvalmode-it.inputvalmode)*st.netlinevalue) as katharh
from fintrade f left join itemtrans it on it.ftrid=f.id
left join material m on m.id=it.iteid
left join storetradelines st on it.stlid=st.id
left join customer c on c.id=f.cusid
where m.code like '73.00.901%' and m.comid=1
group by c.code , year(f.ftrdate)
)a
ORDER BY '],[' + ltrim(str(etos))
FOR XML PATH('')), 1, 2, '') + ']'
SET @query =
'SELECT * FROM
(
select c.code , year(f.ftrdate) as etos , sum((it.outputvalmode-it.inputvalmode)*st.netlinevalue) as katharh
from fintrade f left join itemtrans it on it.ftrid=f.id
left join material m on m.id=it.iteid
left join storetradelines st on it.stlid=st.id
left join customer c on c.id=f.cusid
where m.code like ''73.00.901%'' and m.comid=1
group by c.code , year(f.ftrdate)
) AS t
PIVOT (MAX(katharh) FOR etos IN (' + @years + ')) AS pvt'`
print (@query)
execute (@query)
拿走你的密码
sp_executesql N'
select concat(''select * from sys.objects object_id id in ('',
(select object_id from sys.objects where object_id=1 and object_id=''30000000''),
'') order by object_id'')'
并删除我们得到的sp_executesql的一级嵌套
哪一个当然输出
select * from sys.objects object_id id in () order by object_id
正如你所观察到的
我认为你应该:
sp_executesql @query
拿走你的密码
sp_executesql N'
select concat(''select * from sys.objects object_id id in ('',
(select object_id from sys.objects where object_id=1 and object_id=''30000000''),
'') order by object_id'')'
并删除我们得到的sp_executesql的一级嵌套
哪一个当然输出
select * from sys.objects object_id id in () order by object_id
正如你所观察到的
我认为你应该:
sp_executesql @query
为什么首先需要在这里使用动态sql?从您发布的内容来看,不需要动态sql,也不需要子查询。您只需从customer中选择*,其中comid=1,code='30.00.0000'order by CodeIt是我脚本的一部分,只是为了得到回答。我的主脚本非常大而且动态。所以也许你的整个脚本可以改进。您已经询问了另一个问题,sp_executesql N'select*from customer,其中id在select id from customer,其中comid=1,code=30.00.0000order by code'中应该可以工作,但这是毫无意义的,因为您不需要子查询或sp_executesql,我非常感谢您对我如何思考解决问题的所有评论。。但我不认为这是一个逻辑错误。。这是我想要克服的技术限制。如何生成没有变量的可执行脚本!还有这个话题!为什么首先需要在这里使用动态sql?从您发布的内容来看,不需要动态sql,也不需要子查询。您只需从customer中选择*,其中comid=1,code='30.00.0000'order by CodeIt是我脚本的一部分,只是为了得到回答。我的主脚本非常大而且动态。所以也许你的整个脚本可以改进。您已经询问了另一个问题,sp_executesql N'select*from customer,其中id在select id from customer,其中comid=1,code=30.00.0000order by code'中应该可以工作,但这是毫无意义的,因为您不需要子查询或sp_executesql,我非常感谢您对我如何思考解决问题的所有评论。。但我不认为这是一个逻辑错误。。这是我想要克服的技术限制。如何生成没有变量的可执行脚本!还有这个话题!