English 中文(简体)
我如何从字符串创建 t- sql
原标题:How do I create t-sql from a string

我有一个可变字符串被宣布为变数 :

declare @myGeniuses as nvarchar(8000);
set @myGeniuses =   in (1,2,3) ;

我想在选择的语句中应用我的变量,就像这样:

select * from GENIUSES where GeniusId @myGeniuses
最佳回答

您需要使用 sp_ executesql 来实现让您在查询语句中作为字符串通过并执行的结果 。

详见:http://msdn.microsoft.com/en-us/library/ms188001.aspx

实例使用:

DECLARE @sql AS NVARCHAR(MAX); 
DECLARE @filter AS NVARCHAR(500); 
SET @filter =   in (1,2,3) ; 
SET @sql =  select * from GENIUSES where GeniusId  + @filter; 
EXECUTE sp_executesql @sql; 
问题回答
declare @t nvarchar(max)
set @t= select * from GENIUSES where GeniusId + @myGeniuses

exec @t

你会做。 。 。

exec ( select * from GENIUSES where GeniusId  + @myGeniuses)




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签