declare@namevarchar(250)
declare#aacursorfor
selectnamefromsysobjectswherextypenotin(S,PK,D,X,L)
open#aa
fetchnextfrom#aainto@name
while@@fetch_status=0
begin
printdbo.fgetscript(onlytiancai,sa,sa,database,@name)
fetchnextfrom#aainto@name
end
close#aa
deallocate#aa
4、声明,此函数是csdn邹建邹老迈供应的
3、分开字符串
假如有一个用逗号支解开的字符串,好比说"a,b,c,d,1,2,3,4",怎样用t-sql猎取这个字符串有几个元素,猎取第几个元素的值是几呢?由于t-sql里没有split函数,也没无数组的观点,以是只能本人写几个函数了。
1、猎取元素个数的函数
createfunctiongetstrarrlength(@strvarchar(8000))
returnsint
as
begin
declare@int_returnint
declare@startint
declare@nextint
declare@locationint
select@str=,+@str+,
select@str=replace(@str,,,,,)
select@start=1
select@next=1
select@location=charindex(,,@str,@start)
while(@location0)
begin
select@start=@location+1
select@location=charindex(,,@str,@start)
select@next=@next+1
end
select@int_return=@next-2
return@int_return
end
2、猎取指定索引的值的函数
createfunctiongetstrofindex(@strvarchar(8000),@indexint=0)
returnsvarchar(8000)
as
begin
declare@str_returnvarchar(8000)
declare@startint
declare@nextint
declare@locationint
select@start=1
select@next=1--假如习气从0入手下手则select@next=0
select@location=charindex(,,@str,@start)
while(@location0and@index>@next)
begin
select@start=@location+1
select@location=charindex(,,@str,@start)
select@next=@next+1
end
if@location=0select@location=len(@str)+1--假如是由于没有逗号加入,则以为逗号在字符串后
select@str_return=substring(@str,@start,@location-@start)--@start一定是逗号以后的地位大概就是初始值1
if(@index@next)select@str_return=--假如两者不相称,则是由于逗号太少,大概@index小于@next的初始值1。
return@str_return
end
3、测试
EXECsp_addlinkedserver别号,,MSDASQL,NULL,NULL,DRIVER={SQLServer};SERVER=远程名;UID=用户;PWD=暗码;
execsp_addlinkedsrvlogin@rmtsrvname=别号,@useself=false,@locallogin=sa,@rmtuser=sa,@rmtpassword=暗码
GO
然后你就能够以下:
select*from别号.库名.dbo.表名
insert库名.dbo.表名select*from别号.库名.dbo.表名
select*into库名.dbo.新表名from别号.库名.dbo.表名
go
5、如何猎取一个表中一切的字段信息
蛙蛙保举:如何猎取一个表中一切字段的信息
先创立一个视图
Createviewfielddesc
as
selecto.nameastable_name,c.nameasfield_name,t.nameastype,c.lengthas
length,c.isnullableasisnullable,convert(varchar(30),p.value)asdesp
fromsyscolumnsc
joinsystypestonc.xtype=t.xusertype
joinsysobjectsoono.id=c.id
leftjoinsyspropertiesponp.smallid=c.colidandp.id=o.id
whereo.xtype=U