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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 常大波

{转载} 面试技巧 项目管理知识 程序员九重镜界,很老的今天刚刚翻出来 我来证明越南的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;


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