zoukankan      html  css  js  c++  java
  • Sql Server高手必备

    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, ',') 





  • 相关阅读:
    mysql find_int_set
    PHPSTROM8.0 注册码(7.1也可用)
    gym 101657 D
    gym101657 C
    poj 3525
    poj1279
    poj3335
    poj 1228
    poj 1873
    poj 2074
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050603.html
Copyright © 2011-2022 走看看