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
´Ë為結
Ïà¹ØÎĵµ£º
¾ßÓй̶¨ÓÐЧλÊýºÍСÊýλÊýµÄÊýÖµ×ÊÁÏÀàÐÍ¡£
decimal[ (p[ , s] )] and numeric[ (p[ , s] )]
¹Ì¶¨ÓÐЧλÊýºÍСÊýλÊýµÄÊý×Ö¡£µ±Ê¹ÓÃ×î´óÓÐЧλÊýʱ£¬ÓÐЧֵÊÇ´Ó - 10^38 +1 µ½ 10^38 - 1¡£decimal µÄ SQL-92 ͬÒå×ÖÊÇ dec ºÍ dec(p, s)¡£numeric µÄ¹¦ÄÜÏàµ±ì¶ decimal¡£
p (ÓÐЧλÊý)
¿É´¢´æµÄ×î´óÊ®½øλÊý×ÜÊý£¬Ð¡ ......
¡¶SQL Sever 2005 Êý¾Ý¿â»ù´¡¼°Ó¦Óü¼Êõ½Ì³ÌÓëʵѵ ¡· ÖÜÆæ
SQL Server 2005µÄ°æ±¾£º
1. SQL Server 2005 Enterprise Edition ÆóÒµ°æ(32λºÍ64λ)
ÊÊÓÃÓÚ³¬´óÐÍÆóÒµ¡£Enterprise Edition ÊÇ×îÈ«ÃæµÄSQL Server°æ±¾£¬Êdz¬´óÐÍÆóÒµµÄÀíÏëÑ¡Ôñ£¬Äܹ»Âú×ã×ÔÓµÄÒªÇ󡣸ð汾»¹Í ......
Ê×ÏÈ´ò¿ªSQL Server Management Studio£¬½¨Á¢Ò»¸öÊý¾Ý¿â£¬½¨Á¢ºÃÊý¾Ý¿âºóÑ¡ÔñÄãµÄÊý¾Ý¿âÃû£¬ÓÒ¼ü--ÈÎÎñ--µ¼ÈëÊý¾Ý¿â
´ò¿ªSQLµ¼ÈëºÍµ¼³öÏòµ¼--ÏÂÒ»²½--Êý¾ÝÔ´Ñ¡Ôñ£¨Microsoft Access£©--Ñ¡ÔñÄãµÄACCESSÊý¾Ý¿âÈ»ºóÏÂÒ»²½
¡¡
¹Ø¼üµÄÒ»²½ÔÚ“Ñ¡ÔñÔ´±íºÍÔ´ÊÓͼ”ÕâÀѡÔñ±í--±à¼Ó³Éä--Ñ¡ ......
1
:
replace
º¯Êý
µÚÒ»¸ö²ÎÊýÄãµÄ×Ö·û´®£¬µÚ¶þ¸ö²ÎÊýÄãÏëÌæ»»µÄ²¿·Ö£¬µÚÈý¸ö²ÎÊýÄãÒªÌæ»»³Éʲô
select
replace
(
'
lihan
'
,
'
a
'
,
'
b
'
)
--
---------------------------
lihbn
£¨ËùÓ°ÏìµÄÐÐÊýΪ
1
ÐУ©
==== ......