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

推荐订阅源

Google DeepMind News
Google DeepMind News
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
B
Blog
Martin Fowler
Martin Fowler
Jina AI
Jina AI
I
InfoQ
P
Proofpoint News Feed
小众软件
小众软件
S
SegmentFault 最新的问题
V
V2EX
B
Blog RSS Feed
量子位
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | 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;


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