create table deom
(
id int primary key identity(1,1),
code varchar(32) ,
txt varchar(64)
)
insert into deom(code,txt)
select '102','科學(xué)' union
select '102','數(shù)學(xué)' union
select '102','數(shù)學(xué)1' union
select '101','美術(shù)' union
select '101','體育' union
select '101','體育1'
create function s_str(@code varchar(20))
returns varchar(100)
begin
declare @a varchar(1000)
set @a=''
select @[email protected]+','+txt from deom where [email protected]
set @a=stuff(@a,1,1,'')
return @a
end
go
select code,name=dbo.s_str(code) from deom group by code
效果:

表名前使用一個#號,臨時表是局部的,使用兩個#號,臨時表是全局的,在斷開連接后sql會自動刪除臨時表
create table #a
(
id int,
name varchar(50)
)
insert into #a(id,nam
select * from #a
drop table #a
臨時表除了名稱前多了#號外,其他操作與普通表完全一樣。
tb_Student是已建立好的表,我們通過臨時表temp把tb_Student表中的內(nèi)容復(fù)制到tb_lizi表中,可以使用如下的代碼實(shí)現(xiàn):
use mcf
SELECT * INTO #temp FROM tb_Student
SELECT * INTO tb_lizi FROM #temp
執(zhí)行后斷開sql連接并重新連接(也可以退出sq再l重新啟動sql),發(fā)現(xiàn)tb_lizi表中的內(nèi)容tb_Student表中的內(nèi)容完全一致,實(shí)現(xiàn)了復(fù)制,同時我們沒有用代碼刪除temp表,但mcf數(shù)據(jù)庫中卻沒有temp表了,這是因?yàn)閿嚅_連接時sql自動刪除了temp表
新聞熱點(diǎn)
疑難解答
圖片精選