动态表名的查询SQL CREATE TABLE [dbo].[TB_Ur](
[ID] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[Score] [varchar](50) NULL,
) ON [PRIMARY]
INSERT INTO [dbo].[TB_Ur]
建筑环境与设备工程([ID],[Name] ,[Score])
知识性VALUES (1,'Lucy','');
INSERT INTO [dbo].[TB_Ur]
([ID],[Name] ,[Score])大都督府
VALUES (1,'Lili','');
INSERT INTO [dbo].[TB_Ur]
([ID],[Name] ,[Score])
VALUES (1,'Jack','');
declare @exesql nvarchar(1000);
declare @table varchar(50);
t @table='TB_Ur';
t @exesql='SELECT * FROM [dbo].'+@table;
execute sp_executesql @exesql
呱呱堕地declare @exesql nvarchar(1000);
declare @table varchar(50);
t @table='TB_Ur';瑞雪丰年
t @exesql='update [dbo].'+@table+' t Score=''Good''';
execute sp_executesql @exesql
过程 sp_executesql,参数类型必须为 'ntext/nchar/nvarchar'
另外的例⼦:
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);
/* Build the SQL string one time.*/
SET @SQLString =
N'SELECT * FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @ManagerID';
SET @ParmDefinition = tinyint';
/* Execute the string with the first parameter value. */
SET @IntVariable = 197;
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@ManagerID = @IntVariable;
/* Execute the same string with the cond parameter value. */
SET @IntVariable = 109;
EXECUTE sp_executesql @SQLString, @ParmDefinition,孕妇喝什么奶粉
@ManagerID = @IntVariable;
--------------------------------------------------------------------
DECLARE @IntVariable int;
DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);轻易的反义词
DECLARE @max_title varchar(30);
SET @IntVariable = 197;
SET @SQLString = N'SELECT @max_titleOUT = max(Title)
FROM AdventureWorks.HumanResources.Employee
WHERE ManagerID = @level';
SET @ParmDefinition = tinyint, @max_titleOUT varchar(30) OUTPUT';喝酒胃出血怎么办
EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable, @max_titleOUT=@max_title OUTPUT; SELECT @max_title;
-----------------------------------------------------------
更多的解释请参考微软帮助⽂档。