摘要: --数据 调用declare@snvarchar(max) set@s=N'#T ID NAME DTIME 1 张 2007-12-15 2 刘 2008-10-12 3 王 2009-10-13 4 赵 2009-12-15 5 孙 2009-12-17 6 于 2009-12-14 7 李 2009-12-10 8 高 2009-12-01 9 金 2009-12-10 ' exec #SQL_Script @s ifobject_id('Tempdb..#SQL_Script') isnotnull dropproc #SQL_Scriptgo/**** 阅读全文
posted @ 2010-01-03 14:30 曾祥展 阅读(657) 评论(0) 推荐(0) 编辑
摘要: ifnotobject_id('Tempdb..#T') isnull droptable #TGoCreatetable #T([ID]int,[NAME]nvarchar(1),[DTIME]Datetime)Insert #Tselect1,N'张','2007-12-15'unionallselect2,N'刘','2008-10-12'unionallselect3,N'王','2009-10-13'unionallselect4,N'赵','200 阅读全文
posted @ 2010-01-03 11:31 曾祥展 阅读(759) 评论(0) 推荐(1) 编辑
摘要: createtable tb_1( id int, xiaofei money, userid int, addtime datetime)insertinto tb_1 values(1, 880, 1, getdate())insertinto tb_1 values(2, 950, 2, getdate())insertinto tb_1 values(3, 740, 1, getdate())insertinto tb_1 values(4, 254, 4, getdate())insertinto tb_1 values(5, 2541, 5, getdate())insertint 阅读全文
posted @ 2010-01-03 11:30 曾祥展 阅读(941) 评论(0) 推荐(0) 编辑
摘要: /* ID NUM 1 800 2 855 3 866 4 800 5 844 如何查NUM字段指定数据后一行记录?如NUM字段中800后一条记录 */ createtable tb(ID int, NUM int) insertinto tb values(1 , 800 ) insertinto tb values(2 , 855 ) insertinto tb values(3 , 866 ) insertinto tb values(4 , 800 ) insertinto tb values(5 , 844 ) go --如果ID连续 select m.*from tb m , tb 阅读全文
posted @ 2010-01-03 11:28 曾祥展 阅读(761) 评论(0) 推荐(0) 编辑
摘要: --txt文件分割导入数据库 droptable a; --Step 1:建表 createtable a(a1 varchar(5), a2 varchar(5), a3 varchar(5), a4 varchar(5)); --Step 2:在'C:\'创建一个名为'test.txt'的记事本文件,输入以下内容 a1|a2|a3|a4$$b1|b2|b3|b4$$c1|c2|c3|c4$$ --Setp 3:导入数据:[MRM20090721]是数据库名,[dbo]是对象名, [a]是表名 BULKINSERT[MRM20090721].[dbo].[a] 阅读全文
posted @ 2010-01-03 11:27 曾祥展 阅读(1491) 评论(0) 推荐(0) 编辑
摘要: /*1*/declare@sqlvarchar(8000)set@sql='select''总量''as[时间]'select@sql=@sql+',sum(case时间when'+rtrim(时间)+'then总量end)['+rtrim(时间)+']'from(selectDATEPART(hh,CreateTime)时间,count(*)总量fromBusiness_LoginWHERECreateTime>(selectCONVERT(varchar,getdate(),111))GR 阅读全文
posted @ 2010-01-03 11:22 曾祥展 阅读(1405) 评论(1) 推荐(0) 编辑
摘要: --用PARSENAME函数拆分字符串DECLARE@TTABLE(COL VARCHAR(80))INSERTINTO@TSELECT'123_12_124'UNIONALL SELECT'1234_125_1243'SELECT COL, COL1 =PARSENAME(REPLACE(COL,'_','.'),3), ----替换一下 '.' 因为 parsename 只认 '.' COL2 =PARSENAME(REPLACE(COL,'_','.'),2), 阅读全文
posted @ 2010-01-03 11:19 曾祥展 阅读(4182) 评论(0) 推荐(0) 编辑
摘要: --找出与某id相近的四条记录: declare@tbtable(id int,cName char(10)) insertinto@tb select3,'a'UNIONALL select5,'b'UNIONALL select6,'c'UNIONALL select7,'d'UNIONALL select10,'e'UNIONALL select12,'g'UNIONALL select13,'y'UNIONALL select14,'i'UNIONALL se 阅读全文
posted @ 2010-01-03 11:16 曾祥展 阅读(740) 评论(1) 推荐(0) 编辑
摘要: --备份环境:把数据库服务器(192.168.1.1)的数据库(TEST)备份到客户端(192.168.1.107)的C$下 --首先,做一个与客户端的映射 exec master..xp_cmdshell 'net use z: \\192.168.1.107\e$ "密码" /user:192.168.1.107\administrator' --说明: /* z: 是映射网络路径对应本机的盘符,与下面的备份对应 \\192.168.1.107\e$ 是要映射的网络路径 192.168.1.107\administrator 192.168.1.107是 阅读全文
posted @ 2010-01-03 11:14 曾祥展 阅读(551) 评论(0) 推荐(0) 编辑
摘要: --多行合并2列并去掉重复列: createtable tb (col1 varchar(10), col2 varchar(10), col3 varchar(10), col4 varchar(10)) go insert tb select'11111' , '222' , 'A' , 1 insert tb select'1111' , '333' , 'A' , 1 insert tb select'2222' , '999' , 'B' , 阅读全文
posted @ 2010-01-03 11:13 曾祥展 阅读(548) 评论(0) 推荐(0) 编辑
摘要: --将一个字符串分成多列 declare@strvarchar(100) set@str='111-222-333-444-555-666-777-888' set@str= 'select '''+replace(@str,'-',''',''')+'''' print@str--select '111','222','333','444','555','666','777','888' exe... 阅读全文
posted @ 2010-01-03 11:12 曾祥展 阅读(1358) 评论(0) 推荐(0) 编辑
摘要: --字符串拆分成行 declare@strvarchar(8000) set@str='a1,b1,c2,d1,e3,f5' --,换成 union all select set@str='select name='''+replace(@str,',',''' union all select ''')+'''' exec(@str) /*name ---- a1 b1 c2 d1 e3 f5 */ --字符串分割函数--拆分成多行 createfu 阅读全文
posted @ 2010-01-03 11:10 曾祥展 阅读(4785) 评论(1) 推荐(0) 编辑
摘要: --合并多行的某一列值 --stuff:删除指定的字符,并在指定的起点处插入另一组字符。 createtable tb (id int,col1 varchar(10)) go insert tb select1 , '曾祥展' insert tb select1 , '学无止境' insert tb select1 , 'ok' insert tb select2 , 'B' droptable tb --函数 createfunction StrLink(@idint) returnsvarchar(8000) as begi 阅读全文
posted @ 2010-01-03 11:09 曾祥展 阅读(1530) 评论(0) 推荐(0) 编辑
摘要: -- 逐行计算、逐行递延、逐行更新 declare@tbtable (工号 int, 姓名 nvarchar(10), 数量 int, 基数 int, 开始号 int, 终止号 int) insert@tb(工号, 姓名, 数量) select1, N'张三', 5 insert@tb(工号, 姓名, 数量) select2, N'李四', 6 insert@tb(工号, 姓名, 数量) sel... 阅读全文
posted @ 2010-01-03 11:08 曾祥展 阅读(590) 评论(0) 推荐(0) 编辑
摘要: --用游标 declare@strvarchar(100) --定义游标 declare DZCursor CURSORforSELECT test_str FROM test where test_str='xxx' --打开游标 open DZCursor --从游标取记录 fetchnextfrom DZCursor into@str --当有记录 while@@fetch_status=0 begin insertinto test (test_str) values ('xxx') --取下一条记录 fetchnextfrom DZCursor int 阅读全文
posted @ 2010-01-03 11:06 曾祥展 阅读(2304) 评论(1) 推荐(1) 编辑
摘要: /*表scores有四个字段,学生stu、班级class、学院institute、分数score。 要求返回:班级考试人数大于10、班级最低分在50分以上、计算机学院、班级平均分从高到低前10名。 可以用任意数据库写SQL 返回字段为班级,平均分。*/ --不准嵌套语句。 selecttop10 class,平均分=avg(score) from tb where institute='计算机学院... 阅读全文
posted @ 2010-01-03 11:05 曾祥展 阅读(1280) 评论(0) 推荐(0) 编辑
摘要: --停止数据库的用户连接 createproc killspid (@dbnamevarchar(50)) as declare@sqlnvarchar(1000), @spidint declare getspid cursorfor select spid from sysprocesses where dbid=db_id(@dbname) open getspid fetchnextfrom getspid into@spid while@@fetch_status=0 begin exec('kill '+@spid) fetchnextfrom getspid in 阅读全文
posted @ 2010-01-03 11:05 曾祥展 阅读(1819) 评论(1) 推荐(0) 编辑
摘要: --电话通话次数以及时长 DECLARE@TTABLE (id INT,号码 INT,通话时长 VARCHAR(8)) INSERTINTO@T SELECT1,21981052,'00:01:19'UNIONALL SELECT2,21981052,'00:00:26'UNIONALL SELECT3,21980021,'00:00:56'UNIONALL SELECT4,21980389,'0... 阅读全文
posted @ 2010-01-03 11:03 曾祥展 阅读(2118) 评论(0) 推荐(0) 编辑
摘要: --有小时、分钟,求平均工作时间 declare@tbtable([日期]varchar(3),[工作时间]varchar(5)) insert@tb select'1号','3:10'unionall select'2号','3:20'unionall select'3号','4:20'unionall select'4号','4:30' selectconvert(varchar(5),dateadd(mi,avg(datediff(mi,0,工作时间)),0), 阅读全文
posted @ 2010-01-03 11:03 曾祥展 阅读(979) 评论(0) 推荐(0) 编辑
摘要: --如何向一个自增字段插值 createtable tablename(id intidentity(1,1)) go insertinto tablename defaultvalues go setidentity_insert tablename on insert tablename(id) select11 select*from tablename go --set identity_... 阅读全文
posted @ 2010-01-03 11:02 曾祥展 阅读(1387) 评论(0) 推荐(1) 编辑