---
title: "超精华的ＳＱＬ语句"
author: "water84222"
date: "2008-01-15T17:15:16Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2008-01-15T17:15:16Z"
id: 156530
url: "https://onlyLove.iteye.com/blog/156530"
---

sql server 取日期的年月日部分的函数 

convert(varchar(10),getdate(),120) 

varchar(10) 截取位数可以调节，最多能显示19位（varchar(19)）。 

\----------------------------------

查找存在某些字段的用户表和视图  ![](http://blog.csdn.net/images/star.gif)![](http://blog.csdn.net/images/star_half.gif)  ![CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。](http://blog.csdn.net/images/ask.gif "CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。")

select name,case when xtype='u' then 'usertable' else 'userview' end as xtype  
from sysobjects  
where id in (  
select id from syscolumns where name in('商品编号','商品名称')  
) and (xtype='u' or xtype='v')  
order by xtype

————————————————————————————

字段中的全角数字转换为半角数字  ![](http://blog.csdn.net/images/star.gif)![](http://blog.csdn.net/images/star_half.gif)  ![CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。](http://blog.csdn.net/images/ask.gif "CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。")

建立函数  
create function changestr (@s varchar(100))    
  returns varchar(100) as  
  --字符串中的全角数字转换为半角数字  
  begin    
  set   @s   =   replace(@s,'０','0')    
  set   @s   =   replace(@s,'１','1')    
  set   @s   =   replace(@s,'２','2')    
  set   @s   =   replace(@s,'３','3')    
  set   @s   =   replace(@s,'４','4')    
  set   @s   =   replace(@s,'５','5')    
  set   @s   =   replace(@s,'６','6')    
  set   @s   =   replace(@s,'７','7')    
  set   @s   =   replace(@s,'８','8')    
  set   @s   =   replace(@s,'９','9')    
  return   @s    
  end    
  go    
\----------------------------------------------  
create function changenum(@str nvarchar(4000))    
  returns  decimal  
  --全角数字转换为半角数字   
  as    
  begin    
       declare   @tmp   nvarchar(4000)    
       while   len(@str)&gt;0    
       begin    
            select   @tmp=isnull(@tmp,'')+nchar(unicode(left(@str,1))-65248)    
            select   @str=stuff(@str,1,1,'')    
       end    
       return   @tmp          
  end    
  go    
\----------------------------------------------  
select dbo.changenum('１３２６１０５９３')  
select dbo.changestr('１３２６１０５９３\*\*')

－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－

遍历每个表进行指定操作  ![](http://blog.csdn.net/images/star.gif)![](http://blog.csdn.net/images/star_half.gif)  ![CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。](http://blog.csdn.net/images/ask.gif "CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。")

删除数据库中存在userid字段的表中符合userid='qifei'的所有记录  
use Study

DECLARE @TableName varchar(20)  
DECLARE @sqlstr varchar(100)  
DECLARE Table\_Cursor CURSOR FOR  
select o.name from syscolumns c,sysobjects o  
where c.name='userid' and o.id=c.id and o.xtype='u'  
OPEN Table\_Cursor  
FETCH NEXT FROM  Table\_Cursor INTO @TableName  
WHILE(@@FETCH\_STATUS=0)  
BEGIN  
select @sqlstr='delete from ['+@TableName+'](mailto:'+@TableName+') where userid=''qifei'''  
exec  (@sqlstr)  
FETCH NEXT FROM  Table\_Cursor INTO @TableName  
END  
CLOSE Table\_Cursor  
DEALLOCATE Table\_Cursor

－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－

遍历替换整个数据库中所有用户表的字段中的数据  ![](http://blog.csdn.net/images/star.gif)![](http://blog.csdn.net/images/star.gif)![](http://blog.csdn.net/images/star_half.gif)  ![CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。](http://blog.csdn.net/images/ask.gif "CSDN Blog推出文章指数概念，文章指数是对Blog文章综合评分后推算出的，综合评分项分别是该文章的点击量，回复次数，被网摘收录数量，文章长度和文章类型；满分100，每月更新一次。")

DECLARE @table\_name sysname  
DECLARE @table\_id int  
DECLARE @strSource varchar(20),@strDest varchar(20)  
set @strSource='Source   --要替换掉的字符串  
set @strDest='Dest'           --用来替换的字符串

-- 获得所有User表  
DECLARE tables\_cursor CURSOR FOR select name,id from sysobjects where xtype='U'

OPEN tables\_cursor

-- Perform the first fetch  
FETCH NEXT FROM tables\_cursor INTO @table\_name,@table\_id

-- Check @@FETCH\_STATUS to see if there are any more rows to fetch.  
WHILE @@FETCH\_STATUS = 0  
BEGIN

-- 获取表的文本类型字段  
\-- 'char','nchar','varchar','nvarchar','ntext','text'  
DECLARE columns\_cursor CURSOR FOR select name from syscolumns where id=@table\_id and xtype in (select xtype from systypes where name='varchar')  
DECLARE @column\_name varchar(255)

OPEN columns\_cursor

FETCH NEXT FROM columns\_cursor INTO @column\_name  
WHILE @@FETCH\_STATUS = 0  
BEGIN  
DECLARE @cmd varchar(8000)

-- 遍历表@table\_name的每条记录的字段@column\_name，更新该字段的内容  
\-- 动态生成执行语句  
SET @cmd='  
DECLARE @str\_value varchar(8000)  
DECLARE recorder\_cursor CURSOR FOR select '+@column\_name+' from '+@table\_name+'  
OPEN recorder\_cursor  
FETCH NEXT FROM recorder\_cursor INTO @str\_value  
WHILE @@FETCH\_STATUS = 0  
BEGIN  
SELECT REPLACE(@str\_value,'''+@strSource+''','''+@strDest+''')  
UPDATE '+@table\_name+' SET '+@column\_name+'=@str\_value WHERE CURRENT OF recorder\_cursor

FETCH NEXT FROM recorder\_cursor INTO @str\_value  
END  
CLOSE recorder\_cursor  
DEALLOCATE recorder\_cursor'

Print @cmd  
 --EXEC sp\_executesql @cmd

FETCH NEXT FROM columns\_cursor INTO @column\_name  
END

CLOSE columns\_cursor  
DEALLOCATE columns\_cursor

-- This is executed as long as the previous fetch succeeds.  
FETCH NEXT FROM tables\_cursor INTO @table\_name,@table\_id  
END

CLOSE tables\_cursor  
DEALLOCATE tables\_cursor

\------------------------------------------------------------------------------------------  
csdn会员  zjcxc（邹建）

--1.替换一个表中的所有字段中的ABC为123  

declare   @s   nvarchar(4000),@tbname   sysname    
select    @s=N'',@tbname=N'jobs'     --要处理的表名    
select    @s=@s+N','+quotename(a.name)+N'=replace('+quotename(a.name)+N',N''ABC'',N''123'')'    
from   syscolumns   a,systypes   b    
where   a.id=object\_id(@tbname)      
and   a.xusertype=b.xusertype    
and   b.name   like   N'%char'    
set   @s=stuff(@s,1,1,N'')    
exec(N'update   '+@tbname+'   set   '+@s)  

--2.替换当前数据库的所有表中的所有字段中的ABC为123    
exec   sp\_msforeachtable   @command1=N'    
declare   @s   nvarchar(4000),@tbname   sysname    
select   @s=N'''',@tbname=N''?''    
select   @s=@s+N'',''+quotename(a.name)+N''=replace(''+quotename(a.name)+N'',N''''ABC'''',N''''123'''')''    
from   syscolumns   a,systypes   b    
where   a.id=object\_id(@tbname)      
and   a.xusertype=b.xusertype    
and   b.name   like   N''%char''    
if   @@rowcount&gt;0    
begin    
set   @s=stuff(@s,1,1,N'''')    
exec(N''update   ''+@tbname+''   set   ''+@s)    
end'    
\---------------------------------------------------------------------------------  
csdn会员 chuifengde(树上的鸟儿)

create proc procTab  
@table varchar(100)  
as  
declare @t table(a int,b varchar(20))  
declare @a varchar(1000)

insert @t select distinct b.colid, b.name from sysobjects a Inner Join syscolumns b On a.id=b.id Inner Join systypes c On b.xtype=c.xtype  
where a.id=object\_id(@table) and a.xtype='U' and c.xtype in(34,35,99,167,175,231,239) order by b.colid

select @a=coalesce(@a+',','')+b+'=isnull('+b+','''')' from @t  
select @a='update '+@table+' set '+@a

exec(@a)

go  
\--如果更新整个库中的表则如下  
sp\_msforeachtable 'exec procTab ''?'' '  
\--更新某个表就  
exec procTab '表名'  
\-------------------------------------------------------------------------