DECLARE @providerIdList varchar(400)
DECLARE @q varchar(400)
SELECT @q = ''
SELECT @providerIdList = '(1, 5, 15)'
SET @q = 'SELECT u.Id FROM [user] u
LEFT JOIN Provider p ON u.Provider_FK = p.Id
LEFT JOIN Providers2Users pu ON pu.user_FK = u.Id
LEFT JOIN Provider ap ON ap.Id = pu.provider_fk
WHERE p.Id IN ' + @providerIdList
exec @q
下面這個SQL的錯誤是,當我嘗試了上述我怎樣才能解決這個查詢
Msg 203, Level 16, State 2, Line 18
The name 'SELECT u.Id FROM [user] u
LEFT JOIN Provider p ON u.Provider_FK = p.Id
LEFT JOIN Providers2Users pu ON pu.user_FK = u.Id
LEFT JOIN Provider ap ON ap.Id = pu.provider_fk
WHERE p.Id IN (1, 5, 15)' is not a valid identifier.
我真的很感激,如果顯示的查詢來執行我收到異常有人可以指出我導致這些錯誤的原因
exec(@q)也可以。您只需圍繞在括號 – 2009-06-02 19:56:25
中的@var,這將不起作用:消息214,級別16,狀態2,過程sp_executesql,行1 過程期望類型爲'ntext/nchar/nvarchar'的參數'@statement'。 – 2009-06-02 19:57:54