一些sql试题及答案

Posted on 2008-08-05 16:42  至今  阅读(2188)  评论(0编辑  收藏  举报

1.
比方说某库中有N个存储过程:
select name from sysobjects where xtype = 'P';

name -------------------------------------------------------------------------------------------------------
SO_updateorderdetail
HR_ykf05p_calc
GL_uninit_comp_acct_must
……
……
(N row(s) affected)

那幺如何在不用游标不用循环的情况下用T-SQL语句将这个数据库中的所有存储过程删除?

2.
有一个字符串: a,bc,d,abce,ddz
现在用T-SQL语句把这一字符串变成这种样子:
item
----------
a
bc
d
abce
ddz

(5 row(s) affected)

 

 


第1题:
declare @v_sql varchar(8000)
set @v_sql = ' ' -- 这一句是必须的.
select @v_sql = @v_sql + ',' + name from sysobjects where xtype = 'P'
set @v_sql = 'drop procdure ' + right(@v_sql,len(@v_sql)-2)
print @v_sql
--exec (@v_sql) -- 这一句注释打开的话你就会把当前库中所有的存储过程删掉.

第2题:
declare @str varchar(20),@strSql varchar(8000)
set @str = 'a,bc,d,abce,ddz' -- 此处的字符串可以随心所欲的更改
if object_id('tempdb.dbo.#temp1') is null
create table #temp1(item varchar(20))
else
truncate table #temp1
SELECT @strSql='insert into #temp1 values('''+REPLACE(@str,',',''')
insert into #temp1 values(''')+''')'
print @strSql
exec (@strSql)
select * from #temp1
 "Data Source=.; Initial Catalog=his;Persist Security Info =True;
User Id=sa;PassWord=abc123"

 

sql   中   Table   中一个   int   列
  内容是     1   、2、4、5、6、9、……138、200
共100行纪录,   但是最后一行的值是   200   也就是说肯定是不连续的,用一个sql语句找到第一个断位的数的值,
如上面的序列就是 3


select   top   1   t1.Value+1   from   Table1   t1 
where   not   exists   (   select   1   from   Table1   t2   where   t2.Value   =   t1.Value   +1   )
order   by   t1.Value

 

Copyright © 2024 至今
Powered by .NET 8.0 on Kubernetes