Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL ServerÖеÄÐÐÁе¹Öü¼ÇÉ

ÐÐÁе¹ÖÃÔÚsql serverÖÐÊÇÒ»Öֺܳ£¼ûµÄ¼¼ÇÉ£¬ÔÚ×öÓ¦ÓÃϵͳµÄʱºò£¬¾­³£ÐèÒª×öһЩͳ¼Æ¹¦ÄܱÜÃâ²»ÁËʹÓÃÐÐÁе¹ÖÃÕâ¸ö¼¼ÇÉ£¬ÎÒССµÄ×öÁËÒ»ÏÂ×ܽ᣺
µÚÒ»ÖÖ£ºsql server 2000ÖÐʹÓÃcase½øÐÐÐÐÁе¹ÖÃ
create table RowCellConvertTest
(
 grade varchar(50),
 sex varchar(50),
 studentCount int
)
go
insert into RowCellConvertTest
select 'Ò»Ä꼶','ÄÐ',100 union all
select 'Ò»Ä꼶','Å®',200 union all
select '¶þÄ꼶','ÄÐ',100 union all
select '¶þÄ꼶','Å®',100 union all
select 'ÈýÄ꼶','ÄÐ',200 union all
select 'ÈýÄ꼶','Å®',200
go
select * from RowCellConvertTest
go
--ÀûÓÃSQL Server 2000µÄcase½«ÐÐת»»³ÉÁÐ
declare @sql varchar(max)
set @sql = 'select grade'
select @sql=@sql+ ',sum(case when sex = '''+sex+''' then studentCount else '''' end) as '''+sex+'''' from RowCellConvertTest group by sex
--select @sql = substring(@sql,2,len(@sql))
--print @sql
select  @sql = @sql +'from RowCellConvertTest group by grade'
exec(@sql)
go
drop table RowCellConvertTest
go
     
Ч¹û£º
 
     
     
µÚ¶þÖÖ:
--ÀûÓÃSQL Server 2005ÐÂÌØÐÔpivot½«ÐÐת»»³ÉÁÐ
select grade,ÄÐ,Å®
from
(
select  studentCount, sex, grade from RowCellConvertTest
) p
PIVOT
(
 sum (studentCount)
 FOR sex IN
 (ÄÐ,Å®)
) AS pvt
ORDER BY pvt.grade;
     
     
¸½¼Ó£º½«ÁÐת»»³ÉÐÐ
     
--½«ÁÐת»»³ÉÐÐ
create table CellRowConvertTest
(
 grade varchar(50),
 ÄÐ varchar(50),
 Å® varchar(50)
)
go
insert into CellRowConvertTest
select 'Ò»Ä꼶',100,200 union all
select '¶þÄ꼶',100,100 union all
select 'ÈýÄ꼶',200,200
go
select * from CellRowConvertTest
go
select grade,sex,studentCount
from
(
 select grade,ÄÐ,Å® from CellRowConvertTest
) as p
unpivot
(
 studentCount for sex in (ÄÐ,Å®)
) as unpvt;
go
drop table CellRowConvertTest
go
     
Ч¹û:
 


Ïà¹ØÎĵµ£º

VS2005×Ô´øSQL ExpressÅäÖÃ

ÏÂÃæÊDzÙ×÷²½Ö裺
1. “·þÎñºÍÁ¬½ÓµÄÍâΧӦÓÃÅäÖÓ -¡·“±¾µØÁ¬½ÓºÍÔ¶³ÌÁ¬½Ó”-¡·“ͬʱʹÓÃTCP/IPºÍNamed Pipes“£»
2. “·þÎñÅäÖùÜÀíÆ÷”-¡·“ÍøÂçÅäÖÔ-¡·“SQL ExpressµÄЭÒé“-¡·“IPµØÖ·”£¬½«IpAll Tcp¶Ë¿Ú¸ÄΪ1433£»
3.  ÖØÆô·þ ......

Éî¶È¶Ô±ÈOracleÓëSQL Server——µÚÒ»²¿·Ö

²Ù×÷ϵͳµÄÖ§³Ö °æ±¾ºÍ·¢Ðаæ ʵÀý¡¢Êý¾Ý¿âºÍ±í¿Õ¼ä ʵÀýÃûºÍSID ϵͳÊý¾Ý¿âºÍϵͳ±í¿Õ¼ä
Ò»°ãµÄ¹«Ë¾Í¨³£»áÔÚËûÃǵÄÐÅϢϵͳ¼Ü¹¹ÖÐÒýÈë¶àÖÖÊý¾Ý¿âƽ̨£¬Í¬Ê±ÒýÈëÈýµ½ËÄÖÖ²»Í¬µÄRDBMS½â¾ö·½°¸µÄÖдóÐ͹«Ë¾Ò²²¢²»ÉÙ¼û£¬µ±È»ÕâЩ¹«Ë¾ÀïÃæµÄDBAÃÇͨ³£Ò²ÐèҪͬʱӵÓйÜÀí¶àÖÖ²»Í¬Æ½Ì¨µÄ¼¼ÄÜÁË¡£
Ö»ÔÚÒ»ÖÖƽ̨ÉÏÕ¹¿ª¹¤×÷µÄÊý¾Ý ......

SQL »ù±¾²Ù×÷ ɾ³ý ÐÞ¸Ä Ë¢ÐÂ

SQLµÄ²Ù×÷£º
E.g.
judging-±íÃû
userid, name, password-±íÖеĹؼü×Ö
Ìí¼Ó£º
string sql = "insert into judging (userid,name,password) values('" + userid.Text.Trim() + "','" + name.Text.Trim() + "','" + password.Text.Trim() + "')";
 
ɾ³ý£º
string sql = "delete from judging where userid=" + ......

ɾ³ýsql serverÊý¾Ý¿âÖÐËùÓÐÊý¾Ý

Æäʵɾ³ýÊý¾Ý¿âÖÐÊý¾ÝµÄ·½·¨²¢²»¸´ÔÓ£¬ÎªÊ²Ã´ÎÒ»¹Òª¶à´ËÒ»¾ÙÄØ£¬Ò»ÊÇÎÒÕâÀï½éÉܵÄÊÇɾ³ýÊý¾Ý¿âµÄËùÓÐÊý¾Ý£¬ÒòΪÊý¾ÝÖ®¼ä¿ÉÄÜÐγÉÏ໥ԼÊø¹Øϵ£¬É¾³ý²Ù×÷¿ÉÄÜÏÝÈëËÀÑ­»·£¬¶þÊÇÕâÀïʹÓÃÁË΢ÈíδÕýʽ¹«¿ªµÄsp_MSForEachTable´æ´¢¹ý³Ì¡£
Ò²ÐíºÜ¶à¶ÁÕßÅóÓѶ¼¾­Àú¹ýÕâÑùµÄÊÂÇ飺ҪÔÚ¿ª·¢Êý¾Ý¿â»ù´¡ÉÏÇåÀíÒ»¸ö¿Õ¿â£¬µ«ÓÉÓÚ¶ÔÊý¾Ý¿ ......

SQL Server´æ´¢¹ý³ÌÖÐraiserrorµÄʹÓÃ


Ò» ϵͳԤ¶¨Òå´íÎó´úÂë
SQL Server ÓÐ3831¸öÔ¤¶¨Òå´íÎó´úÂ룬ÓÉmaster.dbo.sysmessages±íά»¤¡£Ã¿Ò»¸ö´íÎó´úÂ붼ÓÐÏàÓ¦µÄ¼¶±ðºÍÃèÊö¡£
´íÎó¶¨ÒåµÄ¼¶±ð´Ó0µ½25¡£20ÒÔÉϵĴíÎó´ú±íÖØ´ó´íÎó£¬Í¨³£ÒâζןôíÎó»áµ¼Ö´洢½ø³ÌÁ¢¿ÌÖÕÖ¹£¬²¢ÇÒËùÓеĿͻ§Á¬½Ó¶¼ÒªÖØгõʼ»¯¡£
·Ç¹Ø¼üÐÔ´íÎóÖ»ÊǽûÖ¹µôµ±Ç°ÔËÐеijÌÐòÐУ¬²¢¼Ì ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ