使用变量内联SQL(MSSQL)

可能会执行类似... MSSQL吗?

@myText变量等于'id = 5'的字符串

SELECT * FROM sometable WHERE ( @myText )
jinhuluntai 回答:使用变量内联SQL(MSSQL)

谢谢大家。在您的帮助下,我像这样使用临时表。...

DECLARE @companies TABLE (comp_code INT)    -- create temporary table
INSERT INTO @companies VALUES (5)           -- ( repeat this line for each additional company you wish to keep )

DELETE FROM tableOne Where (Comp_Code NOT IN (select comp_code from @companies));
DELETE FROM tableTwo Where (Comp_Code NOT IN (select comp_code from @companies));
DELETE FROM tableThree Where (Comp_Code NOT IN (select comp_code from @companies));
-- etc etc
,

您需要使用动态SQL。在SQL Server中,这看起来像:

declare @sql nvarchar(max);

set @sql  = 'SELECT * FROM someTable WHERE ([myText])';

set @SQL = replace(@sql,'[mytext]',@mytext);

exec sp_executesql @sql;
本文链接:https://www.f2er.com/3152649.html

大家都在问