一些t-sql技巧

时间:2022-10-06 00:36:59

    本文的源地址:  
    [1] http://ghd258.cnblogs.com/archive/2006/03/20/354147.html

    [2] http://ghd258.cnblogs.com/archive/2006/07/14/450585.html

    [3 http://ghd258.cnblogs.com/archive/2006/02/12/329288.html

    1 只复制一个表结构,不复制数据

    select top 0 * into [t1] from [t2]

    2 获取数据库中某个对象的创建脚本

    (1) 先用下面的脚本创建一个函数

一些t-sql技巧if   exists ( select   1   from  sysobjects  where  id = object_id ( ' fgetscript ' and   objectproperty (id, ' IsInlineFunction ' ) = 0 )
一些t-sql技巧  
drop   function  fgetscript
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
create   function  fgetscript(
一些t-sql技巧
@servername   varchar ( 50 )      -- 服务器名
一些t-sql技巧
 , @userid   varchar ( 50 ) = ' sa '      -- 用户名,如果为nt验证方式,则为空
一些t-sql技巧
 , @password   varchar ( 50 ) = ''      -- 密码
一些t-sql技巧
 , @databasename   varchar ( 50 )     -- 数据库名称
一些t-sql技巧
 , @objectname   varchar ( 250 )     -- 对象名
一些t-sql技巧
returns   varchar ( 8000 )
一些t-sql技巧
as
一些t-sql技巧
begin
一些t-sql技巧 
declare   @re   varchar ( 8000 )         -- 返回脚本
一些t-sql技巧
  declare   @srvid   int , @dbsid   int         -- 定义服务器、数据库集id
一些t-sql技巧
  declare   @dbid   int , @tbid   int          -- 数据库、表id
一些t-sql技巧
  declare   @err   int , @src   varchar ( 255 ),  @desc   varchar ( 255 -- 错误处理变量
一些t-sql技巧

一些t-sql技巧
-- 创建sqldmo对象
一些t-sql技巧
  exec   @err = sp_oacreate  ' sqldmo.sqlserver ' , @srvid  output
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧
-- 连接服务器
一些t-sql技巧
  if   isnull ( @userid , '' ) = ''   -- 如果是 Nt验证方式
一些t-sql技巧
  begin
一些t-sql技巧  
exec   @err = sp_oasetproperty  @srvid , ' loginsecure ' , 1
一些t-sql技巧  
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧  
exec   @err = sp_oamethod  @srvid , ' connect ' , null , @servername
一些t-sql技巧 
end
一些t-sql技巧 
else
一些t-sql技巧  
exec   @err = sp_oamethod  @srvid , ' connect ' , null , @servername , @userid , @password
一些t-sql技巧
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧
-- 获取数据库集
一些t-sql技巧
  exec   @err = sp_oagetproperty  @srvid , ' databases ' , @dbsid  output
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧
-- 获取要取得脚本的数据库id
一些t-sql技巧
  exec   @err = sp_oamethod  @dbsid , ' item ' , @dbid  output, @databasename
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧
-- 获取要取得脚本的对象id
一些t-sql技巧
  exec   @err = sp_oamethod  @dbid , ' getobjectbyname ' , @tbid  output, @objectname
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧
-- 取得脚本
一些t-sql技巧
  exec   @err = sp_oamethod  @tbid , ' script ' , @re  output
一些t-sql技巧 
if   @err <> 0   goto  lberr
一些t-sql技巧
一些t-sql技巧 
-- print @re
一些t-sql技巧
  return ( @re )
一些t-sql技巧
一些t-sql技巧lberr:
一些t-sql技巧 
exec  sp_oageterrorinfo  NULL @src  out,  @desc  out 
一些t-sql技巧 
declare   @errb   varbinary ( 4 )
一些t-sql技巧 
set   @errb = cast ( @err   as   varbinary ( 4 ))
一些t-sql技巧 
exec  master..xp_varbintohexstr  @errb , @re  out
一些t-sql技巧 
set   @re = ' 错误号:  ' + @re
一些t-sql技巧   
+ char ( 13 ) + ' 错误源:  ' + @src
一些t-sql技巧   
+ char ( 13 ) + ' 错误描述:  ' + @desc
一些t-sql技巧 
return ( @re )
一些t-sql技巧
end
一些t-sql技巧
go
一些t-sql技巧


    (2)用法如下:

一些t-sql技巧print  dbo.fgetscript( ' 服务器名 ' , ' 用户名 ' , ' 密码 ' , ' 数据库名 ' , ' 表名或其它对象名 ' )


    (3)如果要获取库里所有对象的脚本,如如下方式

一些t-sql技巧declare   @name   varchar ( 250 )
一些t-sql技巧
declare  #aa  cursor   for
一些t-sql技巧
select  name  from  sysobjects  where  xtype  not   in ( ' S ' , ' PK ' , ' D ' , ' X ' , ' L ' )
一些t-sql技巧
open  #aa
一些t-sql技巧
fetch   next   from  #aa  into   @name
一些t-sql技巧
while   @@fetch_status = 0
一些t-sql技巧
begin
一些t-sql技巧 
print  dbo.fgetscript( ' onlytiancai ' , ' sa ' , ' sa ' , ' database ' , @name )
一些t-sql技巧 
fetch   next   from  #aa  into   @name
一些t-sql技巧
end
一些t-sql技巧
close  #aa
一些t-sql技巧
deallocate  #aa
一些t-sql技巧


    3 分隔字符串

    如果有一个用逗号分割开的字符串,比如说"a,b,c,d,1,2,3,4",如何用t-sql获取这个字符串有几个元素,获取第几个元素的值是多少呢?因为t-sql里没有split函数,也没有数组的概念,所以只能自己写几个函数了。

    (1) 获取元素个数的函数

 

一些t-sql技巧create   function  getstrarrlength ( @str   varchar ( 8000 ))
一些t-sql技巧
returns   int
一些t-sql技巧
as
一些t-sql技巧
begin
一些t-sql技巧  
declare   @int_return   int
一些t-sql技巧  
declare   @start   int
一些t-sql技巧  
declare   @next   int
一些t-sql技巧  
declare   @location   int
一些t-sql技巧  
select   @str   = ' , ' +   @str   + ' , '
一些t-sql技巧  
select   @str = replace ( @str , ' ,, ' , ' , ' )
一些t-sql技巧  
select   @start   = 1
一些t-sql技巧  
select   @next   = 1  
一些t-sql技巧  
select   @location   =   charindex ( ' , ' , @str , @start )
一些t-sql技巧  
while  ( @location   <> 0 )
一些t-sql技巧  
begin
一些t-sql技巧    
select   @start   =   @location   + 1
一些t-sql技巧    
select   @location   =   charindex ( ' , ' , @str , @start )
一些t-sql技巧    
select   @next   = @next   + 1
一些t-sql技巧  
end
一些t-sql技巧 
select   @int_return   =   @next - 2
一些t-sql技巧 
return   @int_return
一些t-sql技巧
end
一些t-sql技巧


    (2) 获取指定索引的值的函数

 

一些t-sql技巧create   function  getstrofindex ( @str   varchar ( 8000 ), @index   int   = 0 )
一些t-sql技巧
returns   varchar ( 8000 )
一些t-sql技巧
as
一些t-sql技巧
begin
一些t-sql技巧  
declare   @str_return   varchar ( 8000 )
一些t-sql技巧  
declare   @start   int
一些t-sql技巧  
declare   @next   int
一些t-sql技巧  
declare   @location   int
一些t-sql技巧  
select   @start   = 1
一些t-sql技巧  
select   @next   = 1   -- 如果习惯从0开始则select @next =0
一些t-sql技巧
   select   @location   =   charindex ( ' , ' , @str , @start )
一些t-sql技巧  
while  ( @location   <> 0   and   @index   >   @next  )
一些t-sql技巧  
begin
一些t-sql技巧    
select   @start   =   @location   + 1
一些t-sql技巧    
select   @location   =   charindex ( ' , ' , @str , @start )
一些t-sql技巧    
select   @next   = @next   + 1
一些t-sql技巧  
end
一些t-sql技巧  
if   @location   = 0   select   @location   = len ( @str ) + 1   -- 如果是因为没有逗号退出,则认为逗号在字符串后
一些t-sql技巧
   select   @str_return   =   substring ( @str , @start , @location   - @start -- @start肯定是逗号之后的位置或者就是初始值1
一些t-sql技巧
   if  ( @index   <>   @next  )  select   @str_return   =   ''   -- 如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1。
一些t-sql技巧
   return   @str_return
一些t-sql技巧
end
一些t-sql技巧


    (3) 用法如下:

一些t-sql技巧SELECT   [ dbo ] . [ getstrarrlength ] ( ' 1,2,3,4,a,b,c,d ' )
一些t-sql技巧
SELECT   [ dbo ] . [ getstrofindex ] ( ' 1,2,3,4,a,b,c,d ' , 5 )

    4  一条语句执行跨越若干个数据库

    我要在一条语句里操作不同的服务器上的不同的数据库里的不同的表,怎么办呢?
    方法一:

一些t-sql技巧select   *   from   OPENDATASOURCE ( ' SQLOLEDB ' , ' Data Source=远程ip;User ID=sa;Password=密码 ' ).库名.dbo.表名

    方法二:
    先使用联结服务器:

一些t-sql技巧EXEC  sp_addlinkedserver  ' 别名 ' , '' , ' MSDASQL ' , NULL , NULL , ' DRIVER={SQL Server};SERVER=远程名;UID=用户;PWD=密码; '
一些t-sql技巧
exec  sp_addlinkedsrvlogin   @rmtsrvname = ' 别名 ' , @useself = ' false ' , @locallogin = ' sa ' , @rmtuser = ' sa ' , @rmtpassword = ' 密码 '
一些t-sql技巧
GO
一些t-sql技巧

    然后:

一些t-sql技巧select   *   from  别名.库名.dbo.表名
一些t-sql技巧
insert  库名.dbo.表名  select   *   from  别名.库名.dbo.表名
一些t-sql技巧
select   *   into  库名.dbo.新表名  from  别名.库名.dbo.表名
一些t-sql技巧
go


    5 获取一个表中所有的字段信息

    先创建一个视图

一些t-sql技巧Create   view  fielddesc    
一些t-sql技巧
as
一些t-sql技巧
select  o.name  as  table_name,c.name  as  field_name,t.name  as  type,c.length  as  
一些t-sql技巧
一些t-sql技巧length,c.isnullable 
as  isnullable, convert ( varchar ( 30 ),p.value)  as  desp 
一些t-sql技巧
from  syscolumns c  
一些t-sql技巧
join  systypes t  on  c.xtype  =  t.xusertype
一些t-sql技巧
join  sysobjects o  on  o.id = c.id 
一些t-sql技巧
left   join     sysproperties p  on  p.smallid = c.colid  and  p.id = o.id    
一些t-sql技巧
where  o.xtype = ' U '

 

    查询时:

一些t-sql技巧Select   *   from  fielddesc  where  table_name  =   ' 你的表名 '  

 

    还有个更强的语句,是邹建写的,也写出来吧

一些t-sql技巧SELECT  
一些t-sql技巧 (
case   when  a.colorder = 1   then  d.name  else   ''   end ) N ' 表名 ' ,
一些t-sql技巧 a.colorder N
' 字段序号 ' ,
一些t-sql技巧 a.name N
' 字段名 ' ,
一些t-sql技巧 (
case   when   COLUMNPROPERTY ( a.id,a.name, ' IsIdentity ' ) = 1   then   ' ' else   ''   end ) N ' 标识 ' ,
一些t-sql技巧 (
case   when  ( SELECT   count ( * )
一些t-sql技巧 
FROM  sysobjects
一些t-sql技巧 
WHERE  (name  in
一些t-sql技巧           (
SELECT  name
一些t-sql技巧          
FROM  sysindexes
一些t-sql技巧          
WHERE  (id  =  a.id)  AND  (indid  in
一些t-sql技巧                    (
SELECT  indid
一些t-sql技巧                   
FROM  sysindexkeys
一些t-sql技巧                   
WHERE  (id  =  a.id)  AND  (colid  in
一些t-sql技巧                             (
SELECT  colid
一些t-sql技巧                            
FROM  syscolumns
一些t-sql技巧                            
WHERE  (id  =  a.id)  AND  (name  =  a.name)))))))  AND
一些t-sql技巧        (xtype 
=   ' PK ' )) > 0   then   ' '   else   ''   end ) N ' 主键 ' ,
一些t-sql技巧 b.name N
' 类型 ' ,
一些t-sql技巧 a.length N
' 占用字节数 ' ,
一些t-sql技巧 
COLUMNPROPERTY (a.id,a.name, ' PRECISION ' as  N ' 长度 ' ,
一些t-sql技巧 
isnull ( COLUMNPROPERTY (a.id,a.name, ' Scale ' ), 0 as  N ' 小数位数 ' ,
一些t-sql技巧 (
case   when  a.isnullable = 1   then   ' ' else   ''   end ) N ' 允许空 ' ,
一些t-sql技巧 
isnull (e. text , '' ) N ' 默认值 ' ,
一些t-sql技巧 
isnull (g. [ value ] , '' AS  N ' 字段说明 '
一些t-sql技巧
-- into ##tx
一些t-sql技巧

一些t-sql技巧
FROM   syscolumns  a  left   join  systypes b 
一些t-sql技巧
on   a.xtype = b.xusertype
一些t-sql技巧
inner   join  sysobjects d 
一些t-sql技巧
on  a.id = d.id   and   d.xtype = ' U '   and   d.name <> ' dtproperties '
一些t-sql技巧
left   join  syscomments e
一些t-sql技巧
on  a.cdefault = e.id
一些t-sql技巧
left   join  sysproperties g
一些t-sql技巧
on  a.id = g.id  AND  a.colid  =  g.smallid  
一些t-sql技巧
order   by   object_name (a.id),a.colorder
一些t-sql技巧
一些t-sql技巧


    6  时间格式转换问题

    因为新开发的软件需要用一些旧软件生成的一些数据,在时间格式上不统一,只能手工转换,研究了一下午写了三条语句,以前没怎么用过convert函数和case语句,还有"+"操作符在不同上下文环境也会起到不同的作用,把我搞晕了要,不过现在看来是差不多弄好了。

    (1) 把所有"70.07.06"这样的值变成"1970-07-06"

一些t-sql技巧UPDATE  lvshi
一些t-sql技巧
SET  shengri  =   ' 19 '   +   REPLACE (shengri,  ' . ' ' - ' )
一些t-sql技巧
WHERE  (zhiyezheng  =   ' 139770070153 ' )

 

    (2)在"1970-07-06"里提取"70","07","06"

一些t-sql技巧SELECT   SUBSTRING (shengri,  3 2 AS   year SUBSTRING (shengri,  6 2 AS   month
一些t-sql技巧      
SUBSTRING (shengri,  9 2 AS   day
一些t-sql技巧
FROM  lvshi
一些t-sql技巧
WHERE  (zhiyezheng  =   ' 139770070153 ' )

 

    (3)把一个时间类型字段转换成"1970-07-06"
 

一些t-sql技巧UPDATE  lvshi
一些t-sql技巧
SET  shenling  =   CONVERT ( varchar ( 4 ),  YEAR (shenling)) 
一些t-sql技巧      
+   ' - '   +   CASE   WHEN   LEN ( MONTH (shenling))  =   1   THEN   ' 0 '   +   CONVERT ( varchar ( 2 ), 
一些t-sql技巧      
month (shenling))  ELSE   CONVERT ( varchar ( 2 ),  month (shenling)) 
一些t-sql技巧      
END   +   ' - '   +   CASE   WHEN   LEN ( day (shenling))  =   1   THEN   ' 0 '   +   CONVERT ( char ( 2 ), 
一些t-sql技巧      
day (shenling))  ELSE   CONVERT ( varchar ( 2 ),  day (shenling))  END
一些t-sql技巧
WHERE  (zhiyezheng  =   ' 139770070153 ' )

 

    7 分区视图

    分区视图是提高查询性能的一个很好的办法

 

一些t-sql技巧-- 看下面的示例
一些t-sql技巧

一些t-sql技巧
-- 示例表
一些t-sql技巧
create   table  tempdb.dbo.t_10(
一些t-sql技巧id 
int   primary   key   check (id  between   1   and   10 ),name  varchar ( 10 ))
一些t-sql技巧
一些t-sql技巧
create   table  pubs.dbo.t_20(
一些t-sql技巧id 
int   primary   key   check (id  between   11   and   20 ),name  varchar ( 10 ))
一些t-sql技巧
一些t-sql技巧
create   table  northwind.dbo.t_30(
一些t-sql技巧id 
int   primary   key   check (id  between   21   and   30 ),name  varchar ( 10 ))
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 分区视图
一些t-sql技巧
create   view  v_t
一些t-sql技巧
as
一些t-sql技巧
select   *   from  tempdb.dbo.t_10
一些t-sql技巧
union   all
一些t-sql技巧
select   *   from  pubs.dbo.t_20
一些t-sql技巧
union   all
一些t-sql技巧
select   *   from  northwind.dbo.t_30
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 插入数据
一些t-sql技巧
insert  v_t  select   1  , ' aa '
一些t-sql技巧
union    all   select   2  , ' bb '
一些t-sql技巧
union    all   select   11 , ' cc '
一些t-sql技巧
union    all   select   12 , ' dd '
一些t-sql技巧
union    all   select   21 , ' ee '
一些t-sql技巧
union    all   select   22 , ' ff '
一些t-sql技巧
一些t-sql技巧
-- 更新数据
一些t-sql技巧
update  v_t  set  name = name + ' _更新 '   where   right (id, 1 ) = 1
一些t-sql技巧
一些t-sql技巧
-- 删除测试
一些t-sql技巧
delete   from  v_t  where   right (id, 1 ) = 2
一些t-sql技巧
一些t-sql技巧
-- 显示结果
一些t-sql技巧
select   *   from  v_t
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 删除测试
一些t-sql技巧
drop   table  northwind.dbo.t_30,pubs.dbo.t_20,tempdb.dbo.t_10
一些t-sql技巧
drop   view  v_t
一些t-sql技巧
一些t-sql技巧一些t-sql技巧
/**/ /*--测试结果
一些t-sql技巧
一些t-sql技巧id          name       
一些t-sql技巧----------- ---------- 
一些t-sql技巧1           aa_更新
一些t-sql技巧11          cc_更新
一些t-sql技巧21          ee_更新
一些t-sql技巧
一些t-sql技巧(所影响的行数为 3 行)
一些t-sql技巧==
*/

 


    8 树型的实现

 

一些t-sql技巧
一些t-sql技巧
-- 参考
一些t-sql技巧

一些t-sql技巧
-- 树形数据查询示例
一些t-sql技巧--
作者: 邹建
一些t-sql技巧

一些t-sql技巧
-- 示例数据
一些t-sql技巧
create   table   [ tb ] ( [ id ]   int   identity ( 1 , 1 ), [ pid ]   int ,name  varchar ( 20 ))
一些t-sql技巧
insert   [ tb ]   select   0 , ' 中国 '
一些t-sql技巧
union    all    select   0 , ' 美国 '
一些t-sql技巧
union    all    select   0 , ' 加拿大 '
一些t-sql技巧
union    all    select   1 , ' 北京 '
一些t-sql技巧
union    all    select   1 , ' 上海 '
一些t-sql技巧
union    all    select   1 , ' 江苏 '
一些t-sql技巧
union    all    select   6 , ' 苏州 '
一些t-sql技巧
union    all    select   7 , ' 常熟 '
一些t-sql技巧
union    all    select   6 , ' 南京 '
一些t-sql技巧
union    all    select   6 , ' 无锡 '
一些t-sql技巧
union    all    select   2 , ' 纽约 '
一些t-sql技巧
union    all    select   2 , ' 旧金山 '
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 查询指定id的所有子
一些t-sql技巧
create   function  f_cid(
一些t-sql技巧
@id   int
一些t-sql技巧)
returns   @re   table ( [ id ]   int , [ level ]   int )
一些t-sql技巧
as
一些t-sql技巧
begin
一些t-sql技巧 
declare   @l   int
一些t-sql技巧 
set   @l = 0
一些t-sql技巧 
insert   @re   select   @id , @l
一些t-sql技巧 
while   @@rowcount > 0
一些t-sql技巧 
begin
一些t-sql技巧  
set   @l = @l + 1
一些t-sql技巧  
insert   @re   select  a. [ id ] , @l
一些t-sql技巧  
from   [ tb ]  a, @re  b
一些t-sql技巧  
where  a. [ pid ] = b. [ id ]   and  b. [ level ] = @l - 1
一些t-sql技巧 
end
一些t-sql技巧一些t-sql技巧
/**/ /**/ /**/ /*--如果只显示最明细的子(下面没有子),则加上这个删除
一些t-sql技巧 delete a from @re a
一些t-sql技巧 where exists(
一些t-sql技巧  select 1 from [tb] where [pid]=a.[id])
一些t-sql技巧--
*/

一些t-sql技巧 
return
一些t-sql技巧
end
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 调用(查询所有的子)
一些t-sql技巧
select  a. * ,层次 = b. [ level ]   from   [ tb ]  a,f_cid( 2 )b  where  a. [ id ] = b. [ id ]
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧
-- 删除测试
一些t-sql技巧
drop   table   [ tb ]
一些t-sql技巧
drop   function  f_cid
一些t-sql技巧
go
一些t-sql技巧
一些t-sql技巧

 

    9 排序问题

 数据库里有1,2,3,4,5 共5条记录,要用一条sql语句让其排序,使它排列成4,5,1,2,3,怎么写?

一些t-sql技巧CREATE   TABLE   [ t ]  (
一些t-sql技巧 
[ id ]   [ int ]   IDENTITY  ( 1 1 NOT   NULL  ,
一些t-sql技巧 
[ GUID ]   [ uniqueidentifier ]   NULL  
一些t-sql技巧
ON   [ PRIMARY ]
一些t-sql技巧
GO

    下面这句执行5次

一些t-sql技巧insert  t  values  ( newid ())

 

    查看执行结果

一些t-sql技巧select   *   from  t

    (1) 第一种

一些t-sql技巧select   *   from  t
一些t-sql技巧 
order   by   case  id  when   4   then   1
一些t-sql技巧                  
when   5   then   2
一些t-sql技巧                  
when   1   then   3
一些t-sql技巧                  
when   2   then   4
一些t-sql技巧                  
when   3   then   5   end

    (2) 第二种

一些t-sql技巧select   *   from  t  order   by  (id + 2 ) % 6

    (3) 第三种

一些t-sql技巧select   *   from  t  order   by   charindex ( cast (id  as   varchar ), ' 45123 ' )

    (4) 第四种

一些t-sql技巧select   *   from  t
一些t-sql技巧
WHERE  id  between   0   and   5
一些t-sql技巧
order   by   charindex ( cast (id  as   varchar ), ' 45123 ' )

    (5) 第五种

一些t-sql技巧select   *   from  t  order   by   case   when  id  > 3   then  id - 5   else  id  end

    (6) 第六种

一些t-sql技巧select   *   from  t  order   by  id  /   4   desc ,id  asc

    10 一条语句删除一批记录

    首先id列是int标识类类型,然后删除ID值为5,6,8,9,10,11的列,这里的cast函数不能用convert函数代替,而且转换的类型必须是varchar,而不能是char,否则就会执行出你不希望的结果,这里的"5,6,8,9,10,11"可以是你在页面上获取的一个chkboxlist构建成的值,然后用下面的一句就全部删除了,比循环用多条语句高效吧应该。

一些t-sql技巧delete   from   [ fujian ]   where   charindex ( ' , ' + cast ( [ id ]   as   varchar ) + ' , ' , ' , ' + ' 5,6,8,9,10,11, ' + ' , ' ) > 0

    还有一种就是

一些t-sql技巧delete   from  table1  where  id  in ( 1 , 2 , 3 , 4  )

    11 获取子表内的一列数据的组合字符串

    下面这个函数获取05年已经注册了的某个所的律师,唯一一个参数就是事务所的名称,然后返回zhuce字段里包含05字样的所有律师。 

一些t-sql技巧CREATE     FUNCTION  fn_Get05LvshiNameBySuo  ( @p_suo   Nvarchar ( 50 ))
一些t-sql技巧
RETURNS   Nvarchar ( 2000 )
一些t-sql技巧
AS
一些t-sql技巧
BEGIN   
一些t-sql技巧 
DECLARE   @LvshiNames   varchar ( 2000 ),  @name   varchar ( 50 )
一些t-sql技巧 
select   @LvshiNames = ''
一些t-sql技巧 
DECLARE  lvshi_cursor  CURSOR   FOR

    12 让0变成1,1变成0 

一些t-sql技巧declare   @a   int
一些t-sql技巧
set   @a   = 0   -- 初始为0
一些t-sql技巧
select   @a
一些t-sql技巧
set   @a   =   @a ^ 1   -- 把0变成1
一些t-sql技巧
select   @a
一些t-sql技巧
set   @a   =   @a ^ 1   -- 把1变成0
一些t-sql技巧
select   @a

     13  四种方法取表里n到m条纪录

    (1) 第一种 

一些t-sql技巧 如果tablename里没有其他identity列,那么:

 

一些t-sql技巧select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
一些t-sql技巧
set rowcount  n
一些t-sql技巧
select * from 表变量 order by columnname desc

   (2) 第二种  

 

一些t-sql技巧select   top  n  *   from  ( select   top  m  *   from  tablename  order   by  columnname) a  order   by  columnname  desc

   (3) 第三种

一些t-sql技巧 select identity(int) id0,* into #temp from tablename

一些t-sql技巧 取n到m条的语句为:

一些t-sql技巧 select * from #temp where id0 >=n and id0 <= m

一些t-sql技巧 如果你在执行 select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
一些t-sql技巧 exec sp_dboption 你的DB名字,'select into/bulkcopy',true


一些t-sql技巧  
(4) 第四种
一些t-sql技巧 如果表里有identity属性,那么简单:一些t-sql技巧 

一些t-sql技巧select * from tablename where identitycol between n and m 


一些t-sql技巧  14 快速获取表test的记录总数

一些t-sql技巧select  rows  from  sysindexes  where  id  =   object_id ( ' test ' and  indid  in  ( 0 , 1 )

一些t-sql技巧  15 提取数据库内所有表的字段详细说明的SQL语句

一些t-sql技巧SELECT  
一些t-sql技巧(
case   when  a.colorder = 1   then  d.name  else   ''   end ) N ' 表名 '
一些t-sql技巧a.colorder N
' 字段序号 '
一些t-sql技巧a.name N
' 字段名 '
一些t-sql技巧(
case   when   COLUMNPROPERTY ( a.id,a.name, ' IsIdentity ' ) = 1   then   ' ' else   ''  
一些t-sql技巧
end ) N ' 标识 '
一些t-sql技巧(
case   when  ( SELECT   count ( *
一些t-sql技巧
FROM  sysobjects 
一些t-sql技巧
WHERE  (name  in  
一些t-sql技巧(
SELECT  name 
一些t-sql技巧
FROM  sysindexes 
一些t-sql技巧
WHERE  (id  =  a.id)  AND  (indid  in  
一些t-sql技巧(
SELECT  indid 
一些t-sql技巧
FROM  sysindexkeys 
一些t-sql技巧
WHERE  (id  =  a.id)  AND  (colid  in  
一些t-sql技巧(
SELECT  colid 
一些t-sql技巧
FROM  syscolumns 
一些t-sql技巧
WHERE  (id  =  a.id)  AND  (name  =  a.name)))))))  AND  
一些t-sql技巧(xtype 
=   ' PK ' )) > 0   then   ' '   else   ''   end ) N ' 主键 '
一些t-sql技巧b.name N
' 类型 '
一些t-sql技巧a.length N
' 占用字节数 '
一些t-sql技巧
COLUMNPROPERTY (a.id,a.name, ' PRECISION ' as  N ' 长度 '
一些t-sql技巧
isnull ( COLUMNPROPERTY (a.id,a.name, ' Scale ' ), 0 as  N ' 小数位数 '
一些t-sql技巧(
case   when  a.isnullable = 1   then   ' ' else   ''   end ) N ' 允许空 '
一些t-sql技巧
isnull (e. text , '' ) N ' 默认值 '
一些t-sql技巧
isnull (g. [ value ] , '' AS  N ' 字段说明 '  
一些t-sql技巧
FROM  syscolumns a 
一些t-sql技巧
left   join  systypes b 
一些t-sql技巧
on  a.xtype = b.xusertype 
一些t-sql技巧
inner   join  sysobjects d 
一些t-sql技巧
on  a.id = d.id  and  d.xtype = ' U '   and  d.name <> ' dtproperties '  
一些t-sql技巧
left   join  syscomments e 
一些t-sql技巧
on  a.cdefault = e.id 
一些t-sql技巧
left   join  sysproperties g 
一些t-sql技巧
on  a.id = g.id  AND  a.colid  =  g.smallid 
一些t-sql技巧
order   by   object_name (a.id),a.colorder
一些t-sql技巧获取表结构
[ 把 'sysobjects' 替换 成 'tablename' 即可 ]  
一些t-sql技巧
一些t-sql技巧
SELECT   CASE   IsNull (I.name,  ''
一些t-sql技巧
When   ''   Then   ''  
一些t-sql技巧
Else   ' * '  
一些t-sql技巧
End   as  IsPK, 
一些t-sql技巧
Object_Name (A.id)  as  t_name, 
一些t-sql技巧A.name 
as  c_name, 
一些t-sql技巧
IsNull ( SubString (M. text 1 254 ),  '' as  pbc_init, 
一些t-sql技巧T.name 
as  F_DataType, 
一些t-sql技巧
CASE   IsNull ( TYPEPROPERTY (T.name,  ' Scale ' ),  ''
一些t-sql技巧
WHEN   ''   Then   Cast (A.prec  as   varchar
一些t-sql技巧
ELSE   Cast (A.prec  as   varchar +   ' , '   +   Cast (A.scale  as   varchar
一些t-sql技巧
END   as  F_Scale, 
一些t-sql技巧A.isnullable 
as  F_isNullAble 
一些t-sql技巧
FROM  Syscolumns  as  A 
一些t-sql技巧
JOIN  Systypes  as  T 
一些t-sql技巧
ON  (A.xType  =  T.xUserType  AND  A.Id  =   Object_id ( ' sysobjects ' ) ) 
一些t-sql技巧
LEFT   JOIN  ( SysIndexes  as  I 
一些t-sql技巧
JOIN  Syscolumns  as  A1 
一些t-sql技巧
ON  ( I.id  =  A1.id  and  A1.id  =   object_id ( ' sysobjects ' and  (I.status  &   0x800 =   0x800   AND  A1.colid  <=  I.keycnt) ) 
一些t-sql技巧
ON  ( A.id  =  I.id  AND  A.name  =   index_col ( ' sysobjects ' , I.indid, A1.colid) ) 
一些t-sql技巧
LEFT   JOIN  SysComments  as  M 
一些t-sql技巧
ON  ( M.id  =  A.cdefault  and   ObjectProperty (A.cdefault,  ' IsConstraint ' =   1  ) 
一些t-sql技巧
ORDER   BY  A.Colid  ASC
一些t-sql技巧


一些t-sql技巧  16 SQL Server中删除重复数据的几个方法
一些t-sql技巧 
 一些t-sql技巧  
数据库的使用过程中由于程序方面的问题有时候会碰到重复数据,重复数据导致了数据库部分设置不能正确设置。

一些t-sql技巧  (1) 方法一

一些t-sql技巧declare   @max   integer , @id   integer
一些t-sql技巧
declare  cur_rows  cursor  local  for   select  主字段, count ( * from  表名  group   by  主字段  having   count ( * >   1
一些t-sql技巧
open  cur_rows
一些t-sql技巧
fetch  cur_rows  into   @id , @max
一些t-sql技巧
while   @@fetch_status = 0
一些t-sql技巧
begin
一些t-sql技巧
select   @max   =   @max   - 1
一些t-sql技巧
set   rowcount   @max
一些t-sql技巧
delete   from  表名  where  主字段  =   @id
一些t-sql技巧
fetch  cur_rows  into   @id , @max
一些t-sql技巧
end
一些t-sql技巧
close  cur_rows
一些t-sql技巧
set   rowcount   0  

一些t-sql技巧  (2) 方法二

一些t-sql技巧  有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

一些t-sql技巧  〈1〉 对于第一种重复,比较容易解决,使用

一些t-sql技巧select   distinct   *   from  tableName 

一些t-sql技巧  就可以得到无重复记录的结果集。

一些t-sql技巧  如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

一些t-sql技巧select   distinct   *   into  #Tmp  from  tableName
一些t-sql技巧
drop   table  tableName
一些t-sql技巧
select   *   into  tableName  from  #Tmp
一些t-sql技巧
drop   table  #Tmp 

一些t-sql技巧  发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

一些t-sql技巧 〈2〉 这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

一些t-sql技巧 假设有重复的字段为Name, Address,要求得到这两个字段唯一的结果集

一些t-sql技巧select   identity ( int , 1 , 1 as  autoID,  *   into  #Tmp  from  tableName
一些t-sql技巧
select   min (autoID)  as  autoID  into  #Tmp2  from  #Tmp  group   by  Name,autoID
一些t-sql技巧
select   *   from  #Tmp  where  autoID  in ( select  autoID  from  #tmp2) 

一些t-sql技巧 最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

一些t-sql技巧  17 查询分析器不能单步调试的的原因

一些t-sql技巧  具体步骤如下:
一些t-sql技巧  1、将服务器【身份验证】属性设置成【混合模式】(window与sql身份验证)
一些t-sql技巧  2、在【控制面板】中打开【服务】将【MSSQLSERVER】服务打开【属性】,选择【登录】页面,将登录身份设置成服务器本地帐号和该帐号密码,如administrator,密码123;
一些t-sql技巧  3、重新启动sqlserver服务,此时的服务指的是【SQL服务管理器】中的SQL SERVER服务;假设【帐号】设置为administrator
一些t-sql技巧  此时达到的效果是:服务器本地帐号administrator与客户端上的administrator(并且该帐号的密码要与服务器密码相同)可以通过【查询分析器】进行调试;
一些t-sql技巧  如果想让【其他帐号】也能够调试,那么还需要如下设置:
一些t-sql技巧  1、在【服务器】上运行dcomcnfg.exe;
一些t-sql技巧  2、在【默认安全机制】中【默认访问权限】右边点击【编辑默认值】选择允许调试的帐号类型,如users用户类型,sample帐号有包含users组;
一些t-sql技巧  3、重新启动sqlserver服务;
一些t-sql技巧  3、在客户端上创建与服务帐号密码一样的用户,如sample;
一些t-sql技巧  做到这步就可以通过查询分析器的调试功能进行单步调试了。
一些t-sql技巧  注:第二步更改“启动服务帐户”,在第一次登录之前,必须更改用户密码。
一些t-sql技巧  不然,event log:
一些t-sql技巧  以当前密码登录的尝试因下列错误而宣告失败:
一些t-sql技巧  在第一次登录之前,必须更改用户密码。