Sql Server高手必备

来源:互联网 发布:成都蜀美网络 编辑:程序博客网 时间:2024/06/05 14:38

1:  函数返回一个Datatable

  1. 函数 : FunName  
  2. 创建人: Kevin  
  3. 主要功能:返回一个数据集(Table)。  
  4. 运行实例:select * from dbo.FunName(2)  
  5. */  
  6. CREATE FUNCTION FunName (  
  7.   参数列表  
  8. )  
  9. RETURNS @表变量名table    
  10. (  
  11. 字段列表  
  12. )  

2: 以逗号进行分隔返回一个结果集

alter function [dbo].[func_splitstring]
(@str nvarchar(max),@split varchar(10))
returns @t Table (c1 varchar(100))
as
begin
    declare @i int
    declare @s int
    set @i=1
    set @s=1
    while(@i>0)
    begin    
        set @i=charindex(@split,@str,@s)
        if(@i>0)
        begin
            insert @t(c1) values(substring(@str,@s,@i-@s))
        end   
        else begin
            insert @t(c1) values(substring(@str,@s,len(@str)-@s+1))
        end
        set @s = @i + 1   
    end
    return
end


3:重设目前最大 identity 值

一样利用 dbcc 指令, 如下: 
dbcc checkident('product',RESEED,100) 
如此一来, 便能将目前的最大 identity 值指向100, 当然若故意设比目前最大值小时, 係统仍会接受, 但若 identity 遇上重覆资料时(如将 identity 设为 primary key时), 将会发生重大问题, 该 table 变成无法 insert 资料, 因为会发生 primary key violation, 解决方法当然就是将目前的 identity 修復, 直接使用 
dbcc checkident('products', RESEED) 
或 
dbcc checkident('products') 

DBCC CHECKIDENT ('#tmp2',  RESEED, 0) 

4:清空一切数据

truncate table


5:插入一个临时表

insert into #tmp2(Mdd) select * from dbo.func_splitstring(@str, ',') 





原创粉丝点击