我自己记录的Sql笔记

1.怎样把存储过程返回的结果集放入临时表,表,表变量中.
1.bcp 或 BULK INSERT ,instead of ,computer by ,group by with rollup.
2.介绍系统存储过程的帮助:
    mk:@MSITStore:C:\Program%20Files\Microsoft%20SQL%20Server\80\Tools\Books\tsqlref.chm::/ts_sp_00_519s.htm
3.在Google上搜一下    TCP 状态 SYN FIN ACK 图 说明 三次握手
4.哈希表.消息队列.
5.Sql中的加密函数
加密函数:pwdencrypt()
比较函数:pwdcompare()
select @oldpwd='aaa',@newpwd='aaa'
select @en_pwd=pwdencrypt('aaa') '加密
print @en_pwd
select pwdcompare(@newpwd, @en_pwd) '比较密码是否符合,符合返回1,不符合返回0
6.指定行在上面,其它的排序.
    1:
    select * from 你的表 order by charindex(','+cast(style as varchar(10))+',',',200,100,300,'),其他字段
    2:
    select * from 表 order by (case when style = 200 then 1
                                    when style = 100 then 2
                                    when style = 300 then 3
                            end),其他字段
    ----------------------------------------------------
    指定排序.   
    declare @tab table (id varchar(30))
    insert into @tab select '移动'
    insert into @tab select '网通'
    insert into @tab select '联通'

    select * from @tab

    select id from @tab
    order by 
    case when ascii(id)=205 then 1
    else  2 end  desc
    ----------------------------------------------------
7.选出所有的排序规则:
    SELECT *
    FROM ::fn_helpcollations()
8.在SQL中
    1).如何选出所有非的列.如:列A所有不是一的.
    2).如何在一个分组函数中写多个聚合函数.即对于一列有多个聚合类函数 .如:对于《列A》分组列出《列A》,《列A的和》,《满足列B=''的列A的和》。
        --select amount,sum(distinct case when id>1000 then id end) ,    sum(distinct case when id<1000 then id end ) from zhong  group by amount
    3).如果组合了多个结果集,那么结果集会对某一列自动排序。如何将一个特定列提到最前。--即第六个问题.
    4).如果组合了多个结果集,想要去重的结果集,如果在每一个结果集中去重了,那么其它结果集还用去重吗?如果去重只影响了去重的那个结果集,那么在每个结果集中都去重了,整个结果集是不是就没重复的了呢。
        --如果用 Union All 则包括重复行。如果没有指定All,则删除重复行。
    5).树状结构的设计.
    6).如果要对至少成功一条的数据汇总.测试环境如下:
    --在CSDN中开贴 :http://community.csdn.net/Expert/topic/4139/4139155.xml?temp=.4137232
        --首先还是搭建测试环境吧,这样,同仁们也好直接在SQL的查询分析器里测试了.
        declare @tab table (state varchar(30),mobile varchar(30),status varchar(30),msg varchar(30))

        insert into @tab
        select '北京','130','OK','内容1'        union        select '北京','131','OK','内容2'        union        select '北京','131','OK','内容2'        union        select '北京','131','OK','内容2'        union        select '北京','132','Err','内容3'        union        select '北京','132','OK','内容6'        union        select '北京','132','OK','内容6'        union        select '北京','132','OK','内容7'        union        select '北京','138','OK','内容8'        union        select '北京','138','OK','内容9'        union        select '天津','133','OK','内容4'        union        select '天津','134','Err','内容5'        union        select  '其它地区','139','OK','内容10'

        select * from @Tab
        /*
        想选出如下表.

        其中:
        ok1 的意思是 status=ok至少为一条的减重后的手机号数量.
        ok2 的意思是 status=ok至少为二条的减重后的手机号数量.
        ok3 的意思是 status=ok至少为三条的减重后的手机号数量.

        State,ok1,ok2,ok3
        北京,4,2,0
        天津,1,0,0
        其它地区,1,0,0
        */

        答案:
        select a.state,
        sum(case when a.t>0 then 1 else 0 end) as ok1,
        sum(case when a.t>1 then 1 else 0 end) as ok2,
        sum(case when a.t>2 then 1 else 0 end) as ok3
        from
        (
            select state,mobile,count(2) as t from @tab where status='OK' group by state,mobile
        ) a   group by a.state

    7).对上表.分析的问题是 限制条件写在哪? 在count()里,在where里,还是having里?
        --所有全成功的.
        select mobile,count(case when status='err' then 1 end )as cou
        from @tab
        group by mobile
        having count(case when status='err' then 1 end )=0
    8).另外,Exists和In是不同的.
        select null as f into #a union select 1 union select 2
        select f as [#a中] from #a
        select f as [not in的结果]  from #a where f not in (select f from #a)
        select f as [not exists的结果]  from #a as x where not exists (select * from #a where f=x.f)
    9).对上面环境测试In的用法.
        declare @tab1 table (state varchar(30),mobile varchar(30),status varchar(30),msg varchar(30))
        insert into @tab1
        select '北京','130','OK','内容1'        union        select '天津','130','Err','内容1'
        -----如果想选出 省和状态同时出现在@tab1中的数据.
        ----第一种方法,不正确.
        select * from @tab t1
        where state in (select state from @tab1  )
        and status in (select status from @tab1 )
        -----这才是想要的.
        select * from @tab t1
        where state+status in (select state+status from @tab1 )
14.扩展存储过程的例子.

   只要安装了就有例子的.
    http://msdn.microsoft.com/library/psdk/sql/ods_6_con_00_6p9v.htm

    这里给你个msdn里的xp_hello的sample:
    http://msdn.microsoft.com/library/psdk/sql/ods_6_con_01_1ptb.htm
15.SQL中二维表生成树.装入TreeView,树的查找,如果在每个节点上加权限,有父节点权限,则有全部子节点权限.怎么处理?
16.模糊查询的SQL语句
ALTER procedure [dbo].[queryAllDb]( @queryString varchar(50) )
as
begin

declare @sql varchar(3000)
declare @tabID int
declare @flag int
declare @col varchar(30)
declare @where varchar(1000)

DECLARE colData_Cursor CURSOR FOR

--产生要查询的每个SQL的临时表
select tab.object_id as tabid,'select * from ' + sc.name + '.' + tab.name  --+ ' where ' + col.name + ' like ''%' + '' + '%'' ' as sql
from sys.tables as tab
left join sys.schemas as sc on ( tab.schema_id = sc.schema_id )
--left join sys.columns as col on (tab.object_id = col.object_id)

OPEN colData_Cursor;
FETCH NEXT FROM colData_Cursor into @tabID,@sql ;
WHILE @@FETCH_STATUS = 0
   BEGIN
        declare where_Cursor cursor for
        select name from sys.columns  where object_id = @tabID
        open where_Cursor
        set @flag = 0 
        select @where = ''
        fetch next from where_cursor into @col
        while @@fetch_status = 0
        begin
            set @flag = @flag + 1 ;
            if ( @flag = 1 ) begin
                    select @where = @where + ' where ' + @col + ' like ''%' + @queryString + '%'' '
            end else begin
                    select @where = @where +  ' or ' + @col + ' like ''%' + @queryString + '%'' '
            end
            fetch next from where_cursor into @col
 
        end ;
        select @sql = @sql + @where
        close where_Cursor;
        DEALLOCATE where_Cursor;
        exec sys.sp_sqlexec     @sql
        --print @sql
   
        FETCH NEXT FROM colData_Cursor into @tabID, @sql ;
   END;
CLOSE colData_Cursor;

DEALLOCATE colData_Cursor;
 
end


17. SQL中显示数据集的第多少条数据.

posted @ 2009-03-09 01:00  NewSea  阅读(290)  评论(0)    收藏  举报