2015-06-25 172 views
1

我已經聲明瞭一個變量@S,並且我正在存儲一個子查詢值,它正在返回多個記錄,現在我想要的是在select語句中使用它,我可以嗎做到這一點是有可能,在SELECT語句中使用變量

這裏是我的查詢我試圖而是得到一個錯誤必須聲明標量變量「@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 

是有可能做到這一點。任何幫助將不勝感激

+1

究竟是什麼你期待該查詢返回? – mxix

+2

如果您的子查詢返回的值超過1,則查詢將失敗,看起來很有可能通過查詢 – JamieD77

+1

與您詢問的內容無關,但.try在'@ S'前刪除'select'。您在查詢中選擇了兩次。 –

回答

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, @[email protected], @OrderIDOUT=OrderID OUTPUT; 

然後您可以將結果存儲在變量,因爲我已經放映OrderInfoOUT

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) 

最後,你可以加入如根據您的需求時態表類似的表變量。

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 
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 
相關問題