惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
B
Blog
小众软件
小众软件
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
I
InfoQ
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
H
Help Net Security
雷峰网
雷峰网
S
SegmentFault 最新的问题
V
Visual Studio Blog
爱范儿
爱范儿

博客园 - 常大波

{转载} 面试技巧 项目管理知识 程序员九重镜界,很老的今天刚刚翻出来 我来证明越南的ZingChat2是腾讯公司开发的 删除Flash控件的 Flash9e.ocx和FlashUtil9e.exe C#入门代码集 JS脚本判断是否支持Cookie,C#读取设置Cookie SRE(Simple Rule Engine) Document JavaScript工具 - 常大波 - 博客园 SQLite NxBRE 学习笔记1 [转] SQL视图查出SqlServer的数据库字典---适用于 SQL2K 和SQL2005 对于SQL2008不适用 乡音 Delegate 委托 C# 来到深圳找工作。 有关“猫”的设计题目,开阔思维。 获取键盘键值 呼叫中心(CallCenter)的发展 Message Queue(消息队列)介绍与应用---转自CSDN
动态表名的查询SQL
常大波 · 2007-06-19 · via 博客园 - 常大波

CREATE TABLE [dbo].[TB_User](
 [ID] [int] NOT NULL,
 [Name] [varchar](50) NOT NULL,
 [Score] [varchar](50) NULL,
 ) ON [PRIMARY]

INSERT INTO [dbo].[TB_User]
   ([ID],[Name] ,[Score])
 VALUES (1,'Lucy','');
INSERT INTO [dbo].[TB_User]
   ([ID],[Name] ,[Score])
 VALUES (1,'Lili','');
INSERT INTO [dbo].[TB_User]
   ([ID],[Name] ,[Score])
 VALUES (1,'Jack','');


declare @exesql nvarchar(1000);
declare @table varchar(50);
set @table='TB_User';
set @exesql='SELECT *  FROM [dbo].'+@table;
execute sp_executesql @exesql

declare @exesql nvarchar(1000);
declare @table varchar(50);
set @table='TB_User';
set @exesql='update [dbo].'+@table+' set 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 = N'@ManagerID 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 second 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 = N'@level tinyint, @max_titleOUT varchar(30) OUTPUT';

EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable, @max_titleOUT=@max_title OUTPUT;
SELECT @max_title;


-----------------------------------------------------------
更多的解释请参考 微软帮助文档。