摘要: 创建:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Class]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Class]GOCreate TABLE [dbo].[Class] ([Class_Id] [int] NOT NULL ,[Class_Name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,[Parent_ID] [int] 阅读全文
posted @ 2010-01-04 22:20 曾祥展 阅读(5663) 评论(10) 推荐(3) 编辑
摘要: --生成测试数据createtable BOM(ID int,parentID int,sClassName varchar(10))insertinto BOM values(1,0,'1111' )insertinto BOM values(2,1,'1111_1' )insertinto BOM values(3,2,'1111-1-1' )insertinto BOM values(4,3,'1111-1-1-1') insertinto BOM values(5,1,'1111-2' )go--创建用户定 阅读全文
posted @ 2010-01-04 21:07 曾祥展 阅读(811) 评论(0) 推荐(0) 编辑
摘要: using System; using System.Web; u... 阅读全文
posted @ 2010-01-04 11:37 曾祥展 阅读(3149) 评论(0) 推荐(1) 编辑
摘要: --数据 调用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 曾祥展 阅读(656) 评论(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 曾祥展 阅读(754) 评论(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 曾祥展 阅读(937) 评论(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 曾祥展 阅读(755) 评论(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 曾祥展 阅读(1478) 评论(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 曾祥展 阅读(1399) 评论(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 曾祥展 阅读(4175) 评论(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 曾祥展 阅读(738) 评论(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 曾祥展 阅读(548) 评论(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 曾祥展 阅读(547) 评论(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 曾祥展 阅读(4773) 评论(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 曾祥展 阅读(1526) 评论(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 曾祥展 阅读(2297) 评论(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 曾祥展 阅读(1275) 评论(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 曾祥展 阅读(1817) 评论(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 曾祥展 阅读(2109) 评论(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 曾祥展 阅读(974) 评论(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 曾祥展 阅读(1382) 评论(0) 推荐(1) 编辑
摘要: createtable news(id int,bid int, title varchar(10)) insertinto news values(1 , 0 , '分类1') insertinto news values(2 , 0 , '分类2') insertinto news values(3 , 0 , '分类3') insertinto news values(4 , 1 , '文章... 阅读全文
posted @ 2010-01-03 11:01 曾祥展 阅读(1186) 评论(0) 推荐(0) 编辑
摘要: --近期价格处于降价趋势(至少调了3次)的所有商品 ifobject_id('[tb]') isnotnulldroptable[tb] go createtable[tb]([日期]datetime,[商品编码]varchar(6),[价格] numeric(3,1)) insert[tb] select'20091101','033001', 11.9unionall select'20091... 阅读全文
posted @ 2010-01-03 10:58 曾祥展 阅读(654) 评论(0) 推荐(0) 编辑
摘要: createtable[tb]([客户编码]varchar(10),[客户名称]varchar(10),[数量]int) insert[tb] select'001','A',2unionall select'001','A',3unionall select'001','A',4unionall select'002','B',1unionall select'002','B',2 --统计 s... 阅读全文
posted @ 2010-01-03 10:57 曾祥展 阅读(1650) 评论(0) 推荐(0) 编辑
摘要: --列和相减 L3-L2declare @test table(L1 varchar(50),L2 int,L3 int)insert into @testselect 'A1',10,10 union allselect 'A1',10,5 union allselect 'A1',5,5 union allselect 'B1',10,20 union allselect 'B1',5,5 s... 阅读全文
posted @ 2010-01-03 10:55 曾祥展 阅读(763) 评论(0) 推荐(0) 编辑
摘要: ALTERPROCEDURE[dbo].[example1] ( @bookervarchar(100) ) AS declare@strvarchar(1000) set@str='select * from tb_itregister where booker in('+@booker+')' execute (@str) 调用: string booker = "a,b"; booker ... 阅读全文
posted @ 2010-01-03 10:54 曾祥展 阅读(10042) 评论(0) 推荐(1) 编辑
摘要: -- 统计交叉相等两列元祖的次数 去掉重复 ifobject_id('[tb]') isnotnulldroptable[tb] go createtable[tb]([num1]varchar(10),[num2]varchar(10)) insert[tb] select'a','b'unionall select'b','c'unionall select'b','a'unionall se... 阅读全文
posted @ 2010-01-03 10:53 曾祥展 阅读(739) 评论(0) 推荐(0) 编辑
摘要: ifobject_id('[tb]') isnotnulldroptable[tb] go createtable[tb](名称 varchar(10),金额 int,标志 varchar(2),日期 varchar(30))--收入1 支出为2 insert[tb] select'a',100,'1','2009-11-23 13:45' unionall select'b',150,'2','2009-11-22 13:45'unionall select'c', 阅读全文
posted @ 2010-01-03 10:52 曾祥展 阅读(820) 评论(0) 推荐(0) 编辑