如何動態地寫下面的查詢?如何動態地在SQL Server 2008中編寫查詢?
表
empid designation interestes
1 developer,tester cricket,chess
1 developer chess
1 techlead cricket
條件:
IF empid = 1
AND (designation LIKE '%developer%' OR designationLIKE '%techlead%')
OR (interests LIKE '%cricket%').
如何動態地寫上面的查詢,如果指定要發送超過2個,也同樣對interstes。
請告訴我...
編輯存儲過程的代碼:
ALTER PROCEDURE [dbo].[usp_GetDevices]
@id INT,
@designation NVARCHAR (MAX)
AS
BEGIN
declare @idsplat varchar(MAX)
set @idsplat = @UserIds
create table #u1 (id1 varchar(MAX))
set @idsplat = 'insert #u1 select ' + replace(@idsplat, ',', ' union select ')
exec(@idsplat)
Select
id FROM dbo.DevicesList WHERE [email protected] AND designation IN (select id1 from #u1)
END
請讓你的問題更清楚。你想寫動態SQL嗎? – Terry 2012-04-02 04:30:33
我把所有的指定像UI中的複選框。用戶選擇no.of開發人員,測試人員,projectlead,techlead動態來查詢。如何查詢從數據庫獲取相關記錄... – user1237131 2012-04-02 04:32:50
http://stackoverflow.com/questions/9970877/how-to-dynamic-write-the-query-in-sql-server-2008 – user2490832 2014-02-27 08:31:41