【问题标题】:sp_executesql or execute without variablessp_executesql 或不带变量执行
【发布时间】:2017-06-28 14:09:30
【问题描述】:

我想在不使用变量的情况下在 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'
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)

【问题讨论】:

  • 这里为什么首先需要使用动态sql呢?从您发布的内容来看,不需要动态 sql。
  • 你也不需要子查询。你只需要select * from customer where comid=1 and code='30.00.0000' order by code
  • 它只是我脚本的一部分,只是为了得到回答。我的主要脚本也是大型动态的。
  • 所以也许你的整个脚本可以改进。你已经问过XY Problem 否则,sp_executesql N'select * from customer where id in (select id from customer where comid=1 and code=''30.00.0000'')order by code' 应该可以工作但没有意义,因为你不需要子查询或 sp_executesql
  • 伙计们,我很欣赏所有这些关于我如何思考解决问题的方法。但我认为这不是逻辑错误。它是我想要克服的技术限制。如何生成没有变量的可执行脚本!以及主题!

标签: sql-server tsql variables execute sp-executesql


【解决方案1】:

获取您的代码

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 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')

当然是输出

select * from sys.objects object_id id in () order by object_id

如你所见。

我认为你应该这样做:

sp_executesql @query

【讨论】:

    猜你喜欢
    • 2012-08-28
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2011-05-14
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多