简体   繁体   English

SQL向查询添加变量

[英]SQL add a variable to a query

How do I create variables that are specified once and then used in queries later in a script? 如何创建一次指定的变量,然后在以后的脚本查询中使用? These variables may be used multiple times in a query, and in multiple queries in a script. 这些变量可以在查询中多次使用,也可以在脚本的多个查询中多次使用。 I use @x as such a variable in the examples below. 在下面的示例中,我将@x用作此类变量。

What I want to do is something like: 我想做的是这样的:

Declare @Query nvarchar(1000)
Declare @x nvarchar(40)

Set @x = 'test'
Set @Query = 'Select [Name]
                     , ' + @x + ' as [TestCase]
              From mytable'

Exec (@Query)

-- returns "Invalid column name 'test'"

Which returns the error mentioned above. 返回上面提到的错误。 I would like it to achieve the equivalent of: 我希望它达到以下效果:

Declare @Query nvarchar(1000)
Declare @x nvarchar(40)

Set @x = 'test'
Set @Query = 'Select [Name]
                     , ''test'' as [TestCase]
              From mytable'

Exec (@Query)

-- Returns e.g.
-- Name   TestCase
-- Alice  Test
-- Bob    Test

I also note that the following doesn't work and returns the same error as the first: 我还注意到以下内容不起作用,并返回与第一个相同的错误:

Declare @Query nvarchar(1000)
Declare @x nvarchar(40)

Set @x = 'test'
Set @Query = 'Select [Name]
                     , ' + 'test' + ' as [TestCase]
              From mytable'

Exec (@Query)

-- returns "Invalid column name 'test'"

Based on the error and since I'm not trying to use the @x as a column name, but just as a variable, I assume I'm using an invalid implementation of a variable. 基于该错误,并且由于我不尝试将@x用作列名,而只是将其用作变量,因此我假设我正在使用变量的无效实现。

Since you're not trying to use a variable as a column name, you do not need to use dynamic SQL at all. 由于您不打算将变量用作列名,因此根本不需要使用动态SQL。 (Which is a Good Thing(TM) since dynamic SQL should only be used with a great deal of caution due to it being a great attack surface.) (这是一件好事,因为动态SQL的攻击面很大,因此应格外谨慎。)

A simple: 一个简单的:

declare @x nvarchar(40)

set @x = 'test'

select [Name], @x as TestCase
from mytable

will do. 会做。


That being said, if you have a use case for dynamic SQL (again the particular query in question here does not but perhaps an ad-hoc query is being passed in to the procedure), the thing to do would be to pass your variable as a parameter to the query via sp_executesql . 话虽这么说,如果您有一个用于动态SQL的用例(同样,这里所涉及的特定查询没有,但是可能正在向该过程传递一个临时查询),那么要做的就是将变量传递为通过sp_executesql查询的参数。 This is akin to creating a stored procedure with parameters: 这类似于使用参数创建存储过程:

declare @x nvarchar(40)
declare @query nvarchar(1000)

set @x = 'test'

set @query = 'select [Name], @x as TestCase from mytable'

exec sp_executesql @query, N'@x nvarchar(1000)', @x

You were missing quotes. 您缺少引号。 Thats it. 而已。 Try below code. 尝试下面的代码。

 Declare @Query nvarchar(1000)
    Declare @x nvarchar(40)

    Set @x = 'test'
    Set @Query = 'Select [Name]
                         , ''' + @x + ''' as [TestCase]
                  From mytable'

    Exec (@Query)
Declare @Query nvarchar(1000)
Declare @x nvarchar(40)

Set @x = 'test'
Set @Query = 'Select [Name],'++''''+@x+''''+ ' as [TestCase]
              From mytable'

print @query

Output: 输出:
Select [Name],'test' as [TestCase] From mytable 从mytable中选择[名称],“测试”作为[TestCase]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM