表中的动态SQL读取语句 [英] Dynamic SQL reading statements from table

查看:58
本文介绍了表中的动态SQL读取语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用第三方应用程序的后端,它将SQL语句存储在

表中,所以我别无选择,只能使用动态SQL来调用它们(除非

别人知道解决方法......)


问题是,我不能让声明运行正常,我看不到

为什么。如果我执行一个硬编码的变体,比如


DECLARE @sql nvarchar(MAX)

SET @sql =''SELECT foo FROM foostable''< br $>
sp_executesql @sql


我得到:''sp_executesql''附近的语法不正确。


如果我运行


sp_executesql''SELECT foo FROM foostable''


我得到:过程需要参数''@statement''类型''ntext / nchar /

nvarchar''。

我理解,因为它省略了N转换器 - 所以如果我运行


sp_executesql N''SELECT foo FROM foostable''


它很好。我不明白为什么第一个版本失败了。是不是有些

有点隐式转换降级@sql?我使用的CAST

和CONVERT的每个变体都没有效果。


这是SQL Server 2005 SP2。在此先感谢。

解决方案

5月1日下午5:04,downwitch< downwi ... @ gmail.comwrote:





我正在使用第三方应用程序的后端存储SQL语句一个

表,所以我别无选择,只能使用动态SQL来调用它们(除非

其他人知道解决方法......)

>
问题是,我不能让声明正常运行,我看不到

为什么。如果我执行一个硬编码的变体,比如


DECLARE @sql nvarchar(MAX)

SET @sql =''SELECT foo FROM foostable''< br $>
sp_executesql @sql


我得到:''sp_executesql''附近的语法不正确。


如果我运行


sp_executesql''SELECT foo FROM foostable''


我得到:过程需要参数''@statement''类型''ntext / nchar /

nvarchar''。

我理解,因为它省略了N转换器 - 所以如果我运行


sp_executesql N''SELECT foo FROM foostable''


它很好。我不明白为什么第一个版本失败了。是不是有些

有点隐式转换降级@sql?我使用的CAST

和CONVERT的每个变体都没有效果。


这是SQL Server 2005 SP2。提前致谢。



尝试打印你的@sql参数,然后手动触发它,你可能会发现字符串不是你所期望的。无论如何,这是我调试动态sql的标准方式。


5月1日下午5:08,manstein< ; jkelly.ad ... @ gmail.comwrote:


5月1日下午5:04,downwitch< downwi ... @ gmail.comwrote:





我正在使用一个第三方应用程序的后端,它将SQL语句存储在一个

表中,所以我别无选择,只能使用动态SQL来调用它们(除非

某人否则知道一个解决方法...)


问题是,我不能让声明运行正常,我看不到

为什么。如果我执行甚至硬编码的变体,如


DECLARE @sql nvarchar(MAX)

SET @sql =' 'SELECT foo FROM foostable''

sp_executesql @sql


我得到:''sp_executesql''附近的语法不正确。


如果我运行


sp_executesql''SELECT foo FROM foostable ''


我得到:程序期望参数''@statement''类型为''ntext / nchar /

nvarchar''。

我理解,因为它省略了N转换器 - 所以如果我运行


sp_executesql N''SELECT foo FROM foostable''


它很好。我不明白为什么第一个版本失败了。是不是有些

有点隐式转换降级@sql?我使用的CAST

和CONVERT的每个变体都没有效果。


这是SQL Server 2005 SP2。提前致谢。



尝试打印你的@sql参数,然后手动触发它,你可能会发现字符串不是你所期望的。无论如何,那是我的调试动态sql的标准方式.-隐藏引用的文字 -


- 显示引用的文字 -



作为附录,你的(MAX)尺寸是多少?如果它太小而无法容纳字符串中的所有字符,那么你的语句将被截断并且

引发错误。


不,我已经打印过,没关系。没有截断。就像上面的例子

,我甚至无法得到一个简单的简短陈述(我的真实

例子是42个字符),我看不到错误。


5月1日下午5:13,manstein< jkelly.ad ... @ gmail.comwrote:


5月1日下午5:08,manstein< jkelly.ad ... @ gmail.comwrote:


5月1日,5:04 pm,downwitch< downwi ... @ gmail.comwrote:




我正在使用第三方应用程序的后端,它将SQL语句存储在

表,所以我别无选择,只能使用动态SQL来调用它们(除非

其他人知道解决方法...)


Pro嗯,我不能得到正确运行的声明,我看不到

为什么。如果我执行甚至硬编码的变体,如


DECLARE @sql nvarchar(MAX)

SET @sql =''SELECT foo FROM foostable''

sp_executesql @sql


我得到:''sp_executesql''附近的语法不正确。


如果我运行


sp_executesql''SELECT foo FROM foostable''


我得到:程序期望参数''@statement''类型''ntext / nchar /

nvarchar''。

哪个我理解,因为它省略了N转换器 - 所以如果我运行


sp_executesql N''SELECT foo来自foostable''


没关系。我不明白为什么第一个版本失败了。是不是有些

有点隐式转换降级@sql?我使用的CAST

和CONVERT的每个变体都没有效果。


这是SQL Server 2005 SP2。提前致谢。


尝试打印你的@sql参数,然后手动触发它,你好b / b
可能会发现字符串不是你所期望的。无论如何,这是我调试动态sql的标准方式.-隐藏引用文本 -


- 显示引用文本 -



作为附录,你的(MAX)大小是多少?如果它太小而无法容纳字符串中的所有字符,那么您的语句将被截断并且

引发错误。



Hi,

I''m using a 3rd-party app''s back end which stores SQL statements in a
table, so I have no choice but to use dynamic SQL to call them (unless
someone else knows a workaround...)

Problem is, I can''t get the statement to run properly, and I can''t see
why. If I execute even a hard-coded variation like

DECLARE @sql nvarchar(MAX)
SET @sql =''SELECT foo FROM foostable''
sp_executesql @sql

I get: Incorrect syntax near ''sp_executesql''.

If I run

sp_executesql ''SELECT foo FROM foostable''

I get: Procedure expects parameter ''@statement'' of type ''ntext/nchar/
nvarchar''.
which I understand, as it''s omitting the N converter--so if I run

sp_executesql N''SELECT foo FROM foostable''

it''s fine. I don''t understand why the first version fails. Is it some
sort of implicit conversion downgrading @sql? Every variation of CAST
and CONVERT I use has no effect.

This is SQL Server 2005 SP2. Thanks in advance.

解决方案

On May 1, 5:04 pm, downwitch <downwi...@gmail.comwrote:

Hi,

I''m using a 3rd-party app''s back end which stores SQL statements in a
table, so I have no choice but to use dynamic SQL to call them (unless
someone else knows a workaround...)

Problem is, I can''t get the statement to run properly, and I can''t see
why. If I execute even a hard-coded variation like

DECLARE @sql nvarchar(MAX)
SET @sql =''SELECT foo FROM foostable''
sp_executesql @sql

I get: Incorrect syntax near ''sp_executesql''.

If I run

sp_executesql ''SELECT foo FROM foostable''

I get: Procedure expects parameter ''@statement'' of type ''ntext/nchar/
nvarchar''.
which I understand, as it''s omitting the N converter--so if I run

sp_executesql N''SELECT foo FROM foostable''

it''s fine. I don''t understand why the first version fails. Is it some
sort of implicit conversion downgrading @sql? Every variation of CAST
and CONVERT I use has no effect.

This is SQL Server 2005 SP2. Thanks in advance.

Try printing your @sql parameter and then firing it mannually, you
might find that the string is not what you expect. Anyway, that is my
standard way of debugging dynamic sql.


On May 1, 5:08 pm, manstein <jkelly.ad...@gmail.comwrote:

On May 1, 5:04 pm, downwitch <downwi...@gmail.comwrote:


Hi,

I''m using a 3rd-party app''s back end which stores SQL statements in a
table, so I have no choice but to use dynamic SQL to call them (unless
someone else knows a workaround...)

Problem is, I can''t get the statement to run properly, and I can''t see
why. If I execute even a hard-coded variation like

DECLARE @sql nvarchar(MAX)
SET @sql =''SELECT foo FROM foostable''
sp_executesql @sql

I get: Incorrect syntax near ''sp_executesql''.

If I run

sp_executesql ''SELECT foo FROM foostable''

I get: Procedure expects parameter ''@statement'' of type ''ntext/nchar/
nvarchar''.
which I understand, as it''s omitting the N converter--so if I run

sp_executesql N''SELECT foo FROM foostable''

it''s fine. I don''t understand why the first version fails. Is it some
sort of implicit conversion downgrading @sql? Every variation of CAST
and CONVERT I use has no effect.

This is SQL Server 2005 SP2. Thanks in advance.


Try printing your @sql parameter and then firing it mannually, you
might find that the string is not what you expect. Anyway, that is my
standard way of debugging dynamic sql.- Hide quoted text -

- Show quoted text -

as an addendum, what is your (MAX) size? If its too small to hold all
the characters in your string, your statement will be truncated and
raise an error.


No, I have printed it, it''s fine. No truncation. Like my example
above, I can''t even get a simple short statement to work (my real
example is 42 characters), and I don''t see the error.

On May 1, 5:13 pm, manstein <jkelly.ad...@gmail.comwrote:

On May 1, 5:08 pm, manstein <jkelly.ad...@gmail.comwrote:

On May 1, 5:04 pm, downwitch <downwi...@gmail.comwrote:

Hi,

I''m using a 3rd-party app''s back end which stores SQL statements in a
table, so I have no choice but to use dynamic SQL to call them (unless
someone else knows a workaround...)

Problem is, I can''t get the statement to run properly, and I can''t see
why. If I execute even a hard-coded variation like

DECLARE @sql nvarchar(MAX)
SET @sql =''SELECT foo FROM foostable''
sp_executesql @sql

I get: Incorrect syntax near ''sp_executesql''.

If I run

sp_executesql ''SELECT foo FROM foostable''

I get: Procedure expects parameter ''@statement'' of type ''ntext/nchar/
nvarchar''.
which I understand, as it''s omitting the N converter--so if I run

sp_executesql N''SELECT foo FROM foostable''

it''s fine. I don''t understand why the first version fails. Is it some
sort of implicit conversion downgrading @sql? Every variation of CAST
and CONVERT I use has no effect.

This is SQL Server 2005 SP2. Thanks in advance.

Try printing your @sql parameter and then firing it mannually, you
might find that the string is not what you expect. Anyway, that is my
standard way of debugging dynamic sql.- Hide quoted text -

- Show quoted text -


as an addendum, what is your (MAX) size? If its too small to hold all
the characters in your string, your statement will be truncated and
raise an error.



这篇关于表中的动态SQL读取语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆