¶¯Ì¬sqlÓï¾ä»ù±¾Óï·¨
¶¯Ì¬sqlÓï¾ä»ù±¾Óï·¨
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃExecÖ´ÐÐ
eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- ´íÎó,²»»áÌáʾ´íÎ󣬵«½á¹ûΪ¹Ì¶¨ÖµFiledName,²¢·ÇËùÒª¡£
Exec('select ' + @fname + ' from tableName') -- Çë×¢Òâ ¼ÓºÅǰºóµÄ µ¥ÒýºÅµÄ±ßÉϼӿոñ
µ±È»½«×Ö·û´®¸Ä³É±äÁ¿µÄÐÎʽҲ¿É
declare @fname varchar(20)
set @fname = 'FiledName' --ÉèÖÃ×Ö¶ÎÃû
declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- ³É¹¦
exec sp_executesql @s -- ´
Ïà¹ØÎĵµ£º
1.¡¸¿ªÊ¼¡¹²Ëµ¥->ÔËÐÐ
--Æô¶¯sql server 2005 ·þÎñ
net start mssqlserver
--Í£Ö¹sql server 2005 ·þÎñ
net stop mssqlserver
2.¡¸¿ªÊ¼¡¹²Ëµ¥->³ÌÐò->Microsoft SQL Server 2005->ÅäÖù¤¾ß
SQL Server Configuration Manager->SQL Server(MSSQLSERVER) ÓÒ»÷ Æô¶¯·þÎñ³É¹¦ºó£¬×´Ì¬ÏÔʾΪ“ÕýÔÚÔËÐ ......
create table aaa
(
id int primary key ,
name varchar(30) not null
)
create table bbb
(
id int primary key ,
name varchar(30) not null
)
--½»
select * from aaa
where exists
(
select * from bbb where aaa.id=bbb.id and aaa.name = bbb.name
)
--²î
select *
from bbb
where not exists
(
......
Àí½âSQL SERVERÖÐËùÓÐÕߺͼܹ¹µÄÇø±ð
SQL SERVER2005½éÉÜÁ˼ܹ¹£¬¼Ü¹¹Ïà¶ÔÓÚÒÔǰ°æ±¾ÖеĶÔÏóËùÓÐÕß¡£±¾ÎĽ«½âÊÍÕâÁ½ÕßµÄÇø±ð£¬²¢Ï£ÍûÄܽ⿪ÄãÖÁ½ñÈԶԼܹ¹Ò»µãÀ§»ó¡£
¶ÔÏóËùÓÐÕß
ÒªÀí½âËùÓÐÕߺͼܹ¹Ö®¼äµÄÇø±ð£¬ÈÃÎÒÃÇÏÈ»¨µãʱ¼äÀ´¸´Ï°Ò»Ï¶ÔÏóµÄËùÓÐȨ¡£ÔÚSQL SERVER2000»òÒÔǰ°æ±¾Öд´½¨Ò»¸ö¶ÔÏ󣬶ÔÏó±ØÐëÒªÓÐÒ»¸öËù ......
ÔÎijö´¦£ºhttp://www.dingos.cn/index.php?topic=1874.0
¶¨ÒåºÍÓ÷¨
CONVERT() º¯ÊýÊǰÑÈÕÆÚת»»ÎªÐÂÊý¾ÝÀàÐ͵ÄͨÓú¯Êý¡£
CONVERT() º¯Êý¿ÉÒÔÓò»Í¬µÄ¸ñʽÏÔʾÈÕÆÚ/ʱ¼äÊý¾Ý
Óï·¨
CONVERT(data_type(length),data_to_be_converted,style)
data_type(length) ¹æ¶¨Ä¿±êÊý¾ÝÀàÐÍ£¨´øÓпÉÑ¡µÄ³¤¶È£©¡£data_to_be_conv ......
SELECT ID,TITLE,
(SELECT TIMES from CLS_COURSE WHERE CLS_COURSE.ID=CLS_CLASS.COURSEID) AS TIMES,
(SELECT CASE WHEN EXISTS(SELECT * from CLS_ATDC WHERE CLS_ATDC.CLSID=CLS_CLASS.ID ) THEN (SELECT TOP 1 COURSETIME from CLS_ATDC WHERE CLS_ATDC.CLSID=CLS_CLASS.ID ORDER BY COURSETIME DESC) ELSE 0 ......