) returns varchar(8000) as begin declare @re varchar(8000) --返回腳本 declare @srvid int,@dbsid int --定義服務器、數據庫集id declare @dbid int,@tbid int --數據庫、表id declare @err int,@src varchar(255), @desc varchar(255) --錯誤處理變量
--創建sqldmo對象 exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output if @err〈〉0 goto lberr
--連接服務器 if isnull(@userid,'')='' --如果是 Nt驗證方式 begin exec @err=sp_oasetproperty @srvid,'loginsecure',1 if @err〈〉0 goto lberr
exec @err=sp_oamethod @srvid,'connect',null,@servername end else exec @err=sp_oamethod @srvid,'connect',null,@servername,@userid,@password
if @err〈〉0 goto lberr
--獲取數據庫集 exec @err=sp_oagetproperty @srvid,'databases',@dbsid output if @err〈〉0 goto lberr
--獲取要取得腳本的數據庫id exec @err=sp_oamethod @dbsid,'item',@dbid output,@databasename if @err〈〉0 goto lberr
--獲取要取得腳本的對象id exec @err=sp_oamethod @dbid,'getobjectbyname',@tbid output,@objectname if @err〈〉0 goto lberr
--取得腳本 exec @err=sp_oamethod @tbid,'script',@re output if @err〈〉0 goto lberr
--print @re return(@re)
lberr: exec sp_oageterrorinfo NULL, @src out, @desc out declare @errb varbinary(4) set @errb=cast(@err as varbinary(4)) exec master..xp_varbintohexstr @errb,@re out set @re='錯誤號: '+@re +char(13)+'錯誤源: '+@src +char(13)+'錯誤描述: '+@desc return(@re) end go
declare @name varchar(250) declare #aa cursor for select name from sysobjects where xtype not in('S','PK','D','X','L') open #aa fetch next from #aa into @name while @@fetch_status=0 begin print dbo.fgetscript('onlytiancai','sa','sa','database',@name) fetch next from #aa into @name end close #aa deallocate #aa
select * from OPENDATASOURCE('SQLOLEDB','Data Source=遠程ip;User ID=sa;Password=密碼').庫名.dbo.表名
第二種方法: 先使用聯結服務器:
EXEC sp_addlinkedserver '別名','','MSDASQL',NULL,NULL,'DRIVER={SQL Server};SERVER=遠程名;UID=用戶;PWD=密碼;' exec sp_addlinkedsrvlogin @rmtsrvname='別名',@useself='false',@locallogin='sa',@rmtuser='sa',@rmtpassword='密碼' GO
然后你就可以如下:
select * from 別名.庫名.dbo.表名 insert 庫名.dbo.表名 select * from 別名.庫名.dbo.表名 select * into 庫名.dbo.新表名 from 別名.庫名.dbo.表名 go
五、 怎樣獲取一個表中所有的字段信息 蛙蛙推薦:怎樣獲取一個表中所有字段的信息 先創建一個視圖
Create view fielddesc as select o.name as table_name,c.name as field_name,t.name as type,c.length as
length,c.isnullable as isnullable,convert(varchar(30),p.value) as desp from syscolumns c join systypes t on c.xtype = t.xusertype join sysobjects o on o.id=c.id left join sysproperties p on p.smallid=c.colid and p.id=o.id where o.xtype='U'
查詢時:
Select * from fielddesc where table_name = '你的表名'
還有個更強的語句,是鄒建寫的,也寫出來吧
SELECT (case when a.colorder=1 then d.name else '' end) N'表名', a.colorder N'字段序號', a.name N'字段名', (case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'標識', (case when (SELECT count(*) FROM sysobjects WHERE (name in (SELECT name FROM sysindexes WHERE (id = a.id) AND (indid in (SELECT indid FROM sysindexkeys WHERE (id = a.id) AND (colid in (SELECT colid FROM syscolumns WHERE (id = a.id) AND (name = a.name))))))) AND (xtype = 'PK'))〉0 then '√' else '' end) N'主鍵', b.name N'類型', a.length N'占用字節數', COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'長度', isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小數位數', (case when a.isnullable=1 then '√'else '' end) N'允許空', isnull(e.text,'') N'默認值', isnull(g.[value],'') AS N'字段說明' --into ##tx
FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name〈〉'dtproperties' left join syscomments e on a.cdefault=e.id left join sysproperties g on a.id=g.id AND a.colid = g.smallid order by object_name(a.id),a.colorder
UPDATE lvshi SET shengri = '19' + REPLACE(shengri, '.', '-') WHERE (zhiyezheng = '139770070153')
2、在“1970-07-06“里提取“70“,“07“,“06“
SELECT SUBSTRING(shengri, 3, 2) AS year, SUBSTRING(shengri, 6, 2) AS month, SUBSTRING(shengri, 9, 2) AS day FROM lvshi WHERE (zhiyezheng = '139770070153')
3、把一個時間類型字段轉換成“1970-07-06“
UPDATE lvshi SET shenling = CONVERT(varchar(4), YEAR(shenling)) + '-' + CASE WHEN LEN(MONTH(shenling)) = 1 THEN '0' + CONVERT(varchar(2), month(shenling)) ELSE CONVERT(varchar(2), month(shenling)) END + '-' + CASE WHEN LEN(day(shenling)) = 1 THEN '0' + CONVERT(char(2), day(shenling)) ELSE CONVERT(varchar(2), day(shenling)) END WHERE (zhiyezheng = '139770070153')
七、 分區視圖
分區視圖是提高查詢性能的一個很好的辦法
--看下面的示例
--示例表 create table tempdb.dbo.t_10( id int primary key check(id between 1 and 10),name varchar(10))
create table pubs.dbo.t_20( id int primary key check(id between 11 and 20),name varchar(10))
create table northwind.dbo.t_30( id int primary key check(id between 21 and 30),name varchar(10)) go
--分區視圖 create view v_t as select * from tempdb.dbo.t_10 union all select * from pubs.dbo.t_20 union all select * from northwind.dbo.t_30 go
--插入數據 insert v_t select 1 ,'aa' union all select 2 ,'bb' union all select 11,'cc' union all select 12,'dd' union all select 21,'ee' union all select 22,'ff'
--更新數據 update v_t set name=name+'_更新' where right(id,1)=1
--刪除測試 delete from v_t where right(id,1)=2
--顯示結果 select * from v_t go
--刪除測試 drop table northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10 drop view v_t
/**//*--測試結果
id name ----------- ---------- 1 aa_更新 11 cc_更新 21 ee_更新
(所影響的行數為 3 行) ==*/
八、 樹型的實現
--參考
--樹形數據查詢示例 --作者: 鄒建
--示例數據 create table [tb]([id] int identity(1,1),[pid] int,name varchar(20)) insert [tb] select 0,'中國' union all select 0,'美國' union all select 0,'加拿大' union all select 1,'北京' union all select 1,'上海' union all select 1,'江蘇' union all select 6,'蘇州' union all select 7,'常熟' union all select 6,'南京' union all select 6,'無錫' union all select 2,'紐約' union all select 2,'舊金山' go
--查詢指定id的所有子 create function f_cid( @id int )returns @re table([id] int,[level] int) as begin declare @l int set @l=0 insert @re select @id,@l while @@rowcount〉0 begin set @l=@l+1 insert @re select a.[id],@l from [tb] a,@re b where a.[pid]=b.[id] and b.[level]=@l-1 end /**//**//**//*--如果只顯示最明細的子(下面沒有子),則加上這個刪除 delete a from @re a where exists( select 1 from [tb] where [pid]=a.[id]) --*/ return end go
--調用(查詢所有的子) select a.*,層次=b.[level] from [tb] a,f_cid(2)b where a.[id]=b.[id] go
--刪除測試 drop table [tb] drop function f_cid go
九、 排序問題
CREATE TABLE [t] ( [id] [int] IDENTITY (1, 1) NOT NULL , [GUID] [uniqueidentifier] NULL ) ON [PRIMARY] GO
下面這句執行5次
insert t values (newid())
查看執行結果
select * from t
1、 第一種
select * from t order by case id when 4 then 1 when 5 then 2 when 1 then 3 when 2 then 4 when 3 then 5 end
2、 第二種
select * from t order by (id+2)%6
3、 第三種
select * from t order by charindex(cast(id as varchar),'45123')
4、 第四種
select * from t WHERE id between 0 and 5 order by charindex(cast(id as varchar),'45123')
5、 第五種
select * from t order by case when id 〉3 then id-5 else id end