超精华的SQL语句
sql server 取日期的年月日部分的函数
convert(varchar(10),getdate(),120)
varchar(10) 截取位数可以调节,最多能显示19位(varchar(19))。
varchar(10) 截取位数可以调节,最多能显示19位(varchar(19))。
----------------------------------
查找存在某些字段的用户表和视图
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
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
————————————————————————————
字段中的全角数字转换为半角数字
建立函数
create function changestr (@s varchar(100))
returns varchar(100) as
--字符串中的全角数字转换为半角数字
begin
set @s = replace(@s,'0','0')
set @s = replace(@s,'1','1')
set @s = replace(@s,'2','2')
set @s = replace(@s,'3','3')
set @s = replace(@s,'4','4')
set @s = replace(@s,'5','5')
set @s = replace(@s,'6','6')
set @s = replace(@s,'7','7')
set @s = replace(@s,'8','8')
set @s = replace(@s,'9','9')
return @s
end
go
----------------------------------------------
create function changenum(@str nvarchar(4000))
returns decimal
--全角数字转换为半角数字
as
begin
declare @tmp nvarchar(4000)
while len(@str)>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('132610593')
select dbo.changestr('132610593**')
create function changestr (@s varchar(100))
returns varchar(100) as
--字符串中的全角数字转换为半角数字
begin
set @s = replace(@s,'0','0')
set @s = replace(@s,'1','1')
set @s = replace(@s,'2','2')
set @s = replace(@s,'3','3')
set @s = replace(@s,'4','4')
set @s = replace(@s,'5','5')
set @s = replace(@s,'6','6')
set @s = replace(@s,'7','7')
set @s = replace(@s,'8','8')
set @s = replace(@s,'9','9')
return @s
end
go
----------------------------------------------
create function changenum(@str nvarchar(4000))
returns decimal
--全角数字转换为半角数字
as
begin
declare @tmp nvarchar(4000)
while len(@str)>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('132610593')
select dbo.changestr('132610593**')
---------------------------------------
遍历每个表进行指定操作
删除数据库中存在userid字段的表中符合userid='qifei'的所有记录
use Study
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+' where userid=''qifei'''
exec (@sqlstr)
FETCH NEXT FROM Table_Cursor INTO @TableName
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
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+' where userid=''qifei'''
exec (@sqlstr)
FETCH NEXT FROM Table_Cursor INTO @TableName
END
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
------------------------------
遍历替换整个数据库中所有用户表的字段中的数据
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>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 '表名'
-------------------------------------------------------------------------
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>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 '表名'
-------------------------------------------------------------------------