在SELECT语句中使用Variable值

时间:2015-06-25 15:49:01

标签: sql sql-server tsql

我已经声明了变量@S并且我在其中存储了一个子查询值,它返回了多条记录,现在我想要的是在select语句中使用它,我能做到这一点是否可能,< / p>

这是我的查询我在尝试但得到错误必须声明标量变量“@S”

Declare @S AS NVarchar(MAX)
    SET @S = '(SELECT es.FirstName FROM [User] es  WHERE  es.UserId IN (SELECT CustomerUserID FROM OrderInfo))'

SELECT  
        OrderInfoId,
        BorrowerFirstName As ConsumerFirstName,
        BorrowerLastName As ConsumerLastName,
        RequestedURL,
        Requests,
        SELECT @S,
        u.FirstName +'' ''+ u.LastName As Affiliate,
        o.RequestDateTime As DateOfTransaction,
        o.RequestIPAddress As OriginatingIPAddress, 
        o.Requests As Status
from orderInfo o 
inner join [User] u on o.AffiliateId = u.UserId

是否可以这样做。任何帮助将不胜感激

5 个答案:

答案 0 :(得分:2)

如果要将作为单个字符串附加到主查询生成的每一行的子查询结果,那么首先必须初始化@S

Declare @S AS NVarchar(MAX) = ''

然后正确设置它:

SELECT @S = @S + ' ' + es.FirstName 
FROM [User] es  
WHERE  es.UserId IN (SELECT CustomerUserID FROM OrderInfo)

并最终在主查询中使用其内容:

SELECT  
        OrderInfoId,
        BorrowerFirstName As ConsumerFirstName,
        BorrowerLastName As ConsumerLastName,
        RequestedURL,
        Requests,
        (SELECT @S),
        u.FirstName +'' ''+ u.LastName As Affiliate,
        o.RequestDateTime As DateOfTransaction,
        o.RequestIPAddress As OriginatingIPAddress, 
        o.Requests As Status
from orderInfo o 
inner join [User] u on o.AffiliateId = u.UserId

答案 1 :(得分:2)

您最大的问题是您尝试在一列中选择多行。这无法完成。您需要将FirstNames连接成单个值。有几种方法可以做到这一点。一种流行的方法是使用STUFFFOR XML这是一个示例,说明如何将所有与您的子查询匹配的FirstNames放入列中。名称将以逗号分隔。

SELECT
    OrderInfoId,
    BorrowerFirstName AS ConsumerFirstName,
    BorrowerLastName AS ConsumerLastName,
    RequestedURL,
    Requests,
    STUFF((SELECT
            ', ' + es.FirstName
           FROM
            [User] es
           WHERE
            es.UserId IN (SELECT
                            CustomerUserID
                          FROM
                            OrderInfo)
           ORDER BY
            es.FirstName
          FOR
           XML PATH('')
          ),1,2,'') AS CustomerFirstNames,
    u.FirstName + ' ' + u.LastName AS Affiliate,
    o.RequestDateTime AS DateOfTransaction,
    o.RequestIPAddress AS OriginatingIPAddress,
    o.Requests AS Status
FROM
    orderInfo o
    INNER JOIN [User] u ON o.AffiliateId = u.UserId

考虑到您将获得在orderInfo中拥有CustomerID的所有用户名,这似乎有点奇怪。您可能希望通过orderInfo.Id或其他东西过滤子查询。

您确定不仅仅是尝试获取CustomerID的名字吗?您可以再次加入[用户]以获取此信息。

SELECT
    OrderInfoId,
    BorrowerFirstName AS ConsumerFirstName,
    BorrowerLastName AS ConsumerLastName,
    RequestedURL,
    Requests,
    cu.FirstName AS Customer,
    u.FirstName + ' ' + u.LastName AS Affiliate,
    o.RequestDateTime AS DateOfTransaction,
    o.RequestIPAddress AS OriginatingIPAddress,
    o.Requests AS Status
FROM
    orderInfo o
    INNER JOIN [User] u ON o.AffiliateId = u.UserId
    INNER JOIN [User] cu ON o.CustomerUserID = cu.UserId

答案 2 :(得分:0)

你需要使整个sql语句成为一个字符串,并像这样使用sp_executesql:

DECLARE @OrderID int;
SET @ParmDefinition = N'@SubQuery varchar(255), '+
                         '@OrderInfoOUT varchar(30) OUTPUT';
SET @S = '(SELECT es.FirstName FROM [User] es  WHERE  es.UserId IN (SELECT CustomerUserID FROM @OrderInfoOUT = OrderInfo))';
SET @SQL ='        OrderInfoId, '+
            'BorrowerFirstName As ConsumerFirstName, '+
            'BorrowerLastName As ConsumerLastName, '+
            'RequestedURL, '+
            'Requests, '+
             @SubQuery+', '+
            'u.FirstName +''' '''+ u.LastName As Affiliate, '+
            'o.RequestDateTime As DateOfTransaction, '+
            'o.RequestIPAddress As OriginatingIPAddress, '+
            'o.Requests As Status'+
    'from orderInfo o '+
    'inner join [User] u on o.AffiliateId = u.UserId';

sp_executesql @SQL, @ParmDefinition,  @SubQuery=@S, @OrderIDOUT=OrderID OUTPUT;

然后,您可以将结果存储在我已使用OrderInfoOUT

显示的变量中

答案 3 :(得分:0)

您将无法以这种方式运行它。你需要像sp_executesql

这样的东西

答案 4 :(得分:0)

我相信你的问题中有各种各样的概念:动态SQL和表变量。

如果您正在使用SQL Server 2008或uper,则可以选择创建表变量,以便在托管例程运行期间保持活动状态。 您可以定义与此类似的内容:

DECLARE @UserInfo TABLE
( 
    FirstName varchar(50) NOT NULL
); 

按如下方式加载信息:

INSERT @UserInfo
SELECT es.FirstName 
FROM [User] es  
WHERE  es.UserId IN (SELECT CustomerUserID FROM OrderInfo)

最后,您可以根据需要连接类似于时态表的表变量。