¶¯Ì¬SQL»ù±¾Óï·¨
1 :ÆÕͨSQLÓï¾ä¿ÉÒÔÓÃexecÖ´ÐÐ
Select * from tableName
exec('select * from tableName')
exec sp_executesql N'select * from tableName' -- Çë×¢Òâ×Ö·û´®Ç°Ò»¶¨Òª¼ÓN
2:×Ö¶ÎÃû£¬±íÃû£¬Êý¾Ý¿âÃûÖ®Àà×÷Ϊ±äÁ¿Ê±£¬±ØÐëÓö¯Ì¬SQL
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 -- ´Ë¾ä»á±¨´í
declare @s Nvarchar(1000) -- ×¢Òâ´Ë´¦¸ÄΪnvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
exec(@s) -- ³É¹¦
exec sp_executesql @s -- ´Ë¾äÕýÈ·
3. Êä³ö²ÎÊý
declare @num int, @sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--ÈçºÎ½«execÖ´Ðнá¹û·ÅÈë±äÁ¿ÖУ¿
declare @num int, @sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
Ïà¹ØÎĵµ£º
ÊìϤSQL SERVER 2000µÄÊý¾Ý¿â¹ÜÀíÔ±¶¼ÖªµÀ£¬ÆäDTS¿ÉÒÔ½øÐÐÊý¾ÝµÄµ¼Èëµ¼³ö£¬Æäʵ£¬ÎÒÃÇÒ²¿ÉÒÔʹÓÃTransact-SQLÓï¾ä½øÐе¼Èëµ¼³ö²Ù×÷¡£ÔÚTransact-SQLÓï¾äÖУ¬ÎÒÃÇÖ÷ҪʹÓÃOpenDataSourceº¯Êý¡¢OPENROWSET º¯Êý£¬¹ØÓÚº¯ÊýµÄÏêϸ˵Ã÷£¬Çë²Î¿¼SQLÁª»ú°ïÖú¡£ÀûÓÃÏÂÊö·½·¨£¬¿ÉÒÔÊ®·ÖÈÝÒ×µØʵÏÖSQL SERVER¡¢ACCESS¡¢EXCELÊý¾ ......
¼òµ¥Ì¸»ùÓÚSQL SERVER ·ÖÒ³´æ´¢¹ý³ÌµÄÑݽø
×÷ÕߣºÖ£×ô
ÈÕÆÚ£º2006-9-30
Õë¶ÔÊý¾Ý¿âÊý¾ÝÔÚUI½çÃæÉϵķÖÒ³ÊÇÀÏÉú³£Ì¸µÄÎÊÌâÁË£¬ÍøÉϺÜÈÝÒ×ÕÒµ½¸÷Ö֓ͨÓô洢¹ý³Ì”´úÂ룬¶øÇÒÓÐЩ»¹¶¨ÖƲéѯÌõ¼þ£¬¿´ÉÏȥʹÓúܷ½±ã¡£±ÊÕß´òËãͨ¹ý±¾ÎÄÒ²À´¼òµ¥Ì¸Ò»Ï»ùÓÚSQL SERVER 2000µÄ·ÖÒ³´æ´¢¹ý³Ì£¬Í¬Ê±Ì¸Ì¸SQL SER ......
µ±ÄãÕýÔÚ´¦Àí±»¶à¸öÕ¾µã£¨ÏñÔÚ¿ç¹ú¹«Ë¾Ìõ¼þÏ£©Ê¹ÓõĴóÊý¾Ý¿âʱ£¬ÄãÒ²Ðí»áÓöµ½±£Ö¤±í¸ñ¶ÔÏóΨһÐÔÕâÑùµÄ¼¬ÊÖÎÊÌâ¡£ÔÚÕâÖÖÇéÐÎÏ£¬¿ÉÄÜÖ»ÄÜ¿¿UNIQUEIDENTIFIERÀ´½â¾ö¡£UNIQUEIDENTIFIERÊÇÒ»¸ö±£´æÈ«¾ÖΨһ±êʶ·ûµÄGUIDÊý¾ÝÀàÐÍ¡£GUIDÊÇÒ»¸ö±£Ö¤Î¨Ò»µÄ¶þ½øÖÆÊý£¬Òò´Ë¼¸ºõûÓбðµÄ¼ÆËã»ú»á²úÉúͬһ¸öÖµ¡£
GUIDµÄΨһֵÊÇÓÉ¼Æ ......
Ò»£ºC# Á¬½ÓSQLÊý¾Ý¿â
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;
Server=myServerAddress;Database=myDataBase;User ID=myUse ......
SQL Server 2005 ¿ª·¢°æ²»ÔÊÐíÔ¶³ÌÁ¬½Ó¡£
¡¡¡¡ÒªÔÊÐíÔ¶³ÌÁ¬½ÓSQL Server 2005 ,ÐèÒªÍê³ÉÒÔϲ½Ö裺
•ÔÚSQLServer ʵÀýÉÏÆôÓÃÔ¶³ÌÁ¬½Ó¡£
•´ò¿ª·þÎñÆ÷ SQLBrowser ·þÎñ¡£
•ÅäÖ÷À»ðǽÒÔÔÊÐíSQLBrowser ·þÎñºÍSQLServer·þÎñÍøÂçͨÐÅ¡£
¡¡¡¡ÔÚSQLServer ʵÀýÉÏÆôÓÃÔ¶³ÌÁ¬½Ó
1.Ö¸Ïò“¿ªÊ¼->³ÌÐ ......