sqlÁÐÏà¼ÓºÏ²¢
ÐÄÓêÖ®¼Ò
--1. ´´½¨±í£¬Ìí¼Ó²âÊÔÊý¾Ý
CREATE TABLE tb(id int, [value] varchar(10))
INSERT tb SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
--SELECT * from tb
/**//*
id value
----------- ----------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
(5 row(s) affected)
*/
--2 ÔÚSQL2000Ö»ÄÜÓÃ×Ô¶¨Ò庯ÊýʵÏÖ
----2.1 ´´½¨ºÏ²¢º¯Êýfn_strSum£¬¸ù¾ÝidºÏ²¢valueÖµ
GO
CREATE FUNCTION dbo.fn_strSum(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @values varchar(8000)
SET @values = ''
SELECT @values = @values + ',' + value from tb WHERE id=@id
RETURN STUFF(@values, 1, 1, '')
END
GO
-- µ÷Óú¯Êý
SELECT id, VALUE = dbo.fn_strSum(id) from tb GROUP BY id
DROP FUNCTION dbo.fn_strSum
----2.2 ´´½¨ºÏ²¢º¯Êýfn_strSum2£¬¸ù¾ÝidºÏ²¢valueÖµ
GO
CREATE FUNCTION dbo.fn_strSum2(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @values varchar(8000)
SELECT @values = isnull(@values + ',', '') + value from tb WHERE id=@id
RETURN @values
END
GO
-- µ÷Óú¯Êý
SELECT id, VALUE = dbo.fn_strSum2(id) from tb GROUP BY id
DROP FUNCTION dbo.fn_strSum2
--3 ÔÚSQL2005ÖеÄнⷨ
----3.1 ʹÓÃOUTER APPLY
SELECT *
from (SELECT DISTINCT id from tb) A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value from tb N
WHERE id = A.id
FOR XML AUTO
), '<N value="', ','), '"/>', ''), 1, 1, '')
)N
----3.2 ʹÓÃXML
SELECT id, [values]=STUFF((SELECT ','+[value] from tb t WHERE id=tb.id FOR XML PATH('')), 1, 1, '')
from tb
GROUP BY id
--4 ɾ³ý²âÊÔ±ítb
drop table tb
/**//*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc
(2 row(s) affected)
*/
½âÊÍÒ»ÏÂsutff()º¯Êý
STUFF ( cha racter_expression , start , length ,character_expression )
SELECT STUFF('abcdef', 2, 3, 'ijklmn')
GO
´Ë為結
Ïà¹ØÎĵµ£º
Ò»¸ö×î¼òµ¥µÄ´úÂë¶Î£º
string sql = string.Format("select Consult_Info.CName,Consult_Record.RTime,Consult_Record.RContent,Emp_Info.EName,CMode.Mode from Consult_Info inner join Consult_Record on(Consult_Info.CID = Consult_Record.CID) inner join Emp_Info on(Emp_Info.EID=Consult_Record.EID) inner join ......
select 1 from table;Óëselect anycol(Ä¿µÄ±í¼¯ºÏÖеÄÈÎÒâÒ»ÐУ© from table;Óëselect * from table ´Ó×÷ÓÃÉÏÀ´ËµÊÇûÓвî±ðµÄ£¬¶¼ÊDz鿴ÊÇ·ñÓмǼ£¬Ò»°ãÊÇ×÷Ìõ¼þ²éѯÓõġ£select 1 from ÖеÄ1ÊÇÒ»³£Á¿£¨¿ÉÒÔΪÈÎÒâÊýÖµ£©£¬²éµ½µÄËùÓÐÐеÄÖµ¶¼ÊÇËü£¬µ«´ÓЧÂÊÉÏÀ´Ëµ£¬1>anycol>*£¬ÒòΪ²»Óòé×Öµä±í¡£ ......
SQL Server 7.0 ÈëÃÅ(7)
ʱ¼ä£º2001/02/06 11:57 ×÷Õߣº¶¹¶¹ CHINAASP
¡¡ &nbs ......
¡¶SQL Sever 2005 Êý¾Ý¿â»ù´¡¼°Ó¦Óü¼Êõ½Ì³ÌÓëʵѵ ¡· ÖÜÆæ
SQL Server 2005µÄ°æ±¾£º
1. SQL Server 2005 Enterprise Edition ÆóÒµ°æ(32λºÍ64λ)
ÊÊÓÃÓÚ³¬´óÐÍÆóÒµ¡£Enterprise Edition ÊÇ×îÈ«ÃæµÄSQL Server°æ±¾£¬Êdz¬´óÐÍÆóÒµµÄÀíÏëÑ¡Ôñ£¬Äܹ»Âú×ã×ÔÓµÄÒªÇ󡣸ð汾»¹Í ......
¡í1:È¡µÃµ±Ç°ÈÕÆÚÊDZ¾Ôµĵڼ¸ÖÜ
SQL> select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from
dual;
TO_CHAR(SYSDATE,'YY
-------------------
20030327 4 18:16:09
SQL> select to_char(sysdate,'W') from dual;
T
-
4 ......