随笔分类 - SQL Server
摘要:1 create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100)) 2 3 declare @name varchar(100) 4 declare cur cursor ...
阅读全文
摘要:BEGIN TRYEND TRYBEGIN CATCHSET @InfoBar=ERROR_MESSAGE()SET @Severity=16EXEC dbo.RaiseErrorSp @Infobar = @InfoBar, @Severity = @Severity, -- int@State
阅读全文
摘要:1 ALTER PROC [dbo].[DBUnfinishedJobrouteSP] 2 ( 3 @Wc WcType,--工作中心 4 @Job JobType=null --作业单 5 ) 6 AS 7 BEGIN 8 9 --结果返回表 10 DECLARE @DBUnfinishedJobroute TABLE( 11 ...
阅读全文
摘要:1 1、UPDATE a SET WtNo=b.NO 2 from WT_Task a 3 INNER JOIN WT_BasicInformation b ON a.WtId=b.ID; 4 2、UPDATE a SET a.WtNo=b.NO 5 FROM WT_Task a,WT_BasicInformation b 6 WHERE a.WtId=b.ID;
阅读全文
摘要:1 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 2 3 1、 delete from t1 where 条件 4 5 2、delete t1 from t1 where 条件 6 7 3、 delete t1 from t1,t2 where 条件 8 9 4、delete t1,t2 from t1,t2 where 条件 10...
阅读全文
摘要:1 USE [Test] 2 GO 3 /****** Object: Trigger [dbo].[shipment_mstInsert] Script Date: 2017/6/22 19:11:16 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 ALTER TRI...
阅读全文
摘要:1 USE [Test] 2 GO 3 /****** Object: Trigger [dbo].[DBpoitem_mstIup] Script Date: 2017/6/1 9:35:41 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 9 ALTER TRI...
阅读全文
摘要:1 老大------drop 2 3 出没场合:drop table tb --tb表示数据表的名字,下同 4 5 绝招:删除内容和定义,释放空间。简单来说就是把整个表去掉.以后要新增数据是不可能的,除非新增一个表, 6 7 例如:一个班就是一个表,学生就是表中的数据,学生的职务就是定义 8 9 drop table class,就是把整个...
阅读全文
摘要:1、环境 数据库为Sql Server2008。 表(Course)结构为: No Char(10) Primary Key Name Varchar(20) Comment Varchar(50) 2、存储过程 就以插入数据为例,其他的可以照着写就行了。 编程语言都有异常的捕获与处理, 在 SqlServer2008 中也是这样子的。 对会出现异常的语句加上 begin try……end tr...
阅读全文
摘要:1 SELECT WorkerNo, DutyTime, DATENAME(weekday, DutyTime) AS WeekDay, CycleType, CycleNumber, YnOnDuty, 2 CASE WHEN YnOnDuty = 0 THEN '休息' ELSE RIGHT('0' + CAST((CASE WHEN (...
阅读全文
摘要:1 SQL的表达式,除了IS NULL和NOT NULL以外,只要出现NULL值结果都为FALSE 2 3 简单的例子: 4 SELECT * FROM table WHERE name!='abc' 5 6 只要name值是NULL,无论用name='abc'还是name!='abc',都不能获得这行,需要获取所有不是'abc'的行应该使用下面的语句: 7 SELECT * FROM ta...
阅读全文
摘要:1 insert into A表(字段1,字段2) select 字段1,字段2 From B表 2 [注:字段类型、字段数应相同] 3 4 --批量进行修改ID值 5 declare @i int 6 set @i=1 7 while @i<197 8 begin 9 update ComputerEquipmentListVIEW set ComputerID='...
阅读全文
摘要:1 ''按多个字段排序 2 Select * From Job order by job desc,id asc 3 4 ''按首字符(非数字)排序 5 Select * From Job order by case when isnumeric(left(jobno,1))=0 6 Then left(jobno,1) end 7 8 ''按首字符分组 9 Select ...
阅读全文
摘要:1 1、if语句使用示例: 2 declare @a int 3 set @a=12 4 if @a>100 5 begin 6 print @a 7 end 8 else 9 begin 10 print 'no' 11 ...
阅读全文
摘要:1 ALTER function [dbo].[fn_getdate3] 2 ( 3 @cc varchar(15) 4 ) 5 RETURNS 6 @Table_Var TABLE 7 ( 8 LastTime datetime 9 ) 10 as 11 begin 12 Declare @rand float 13 Declare @num Int 14...
阅读全文
摘要:1 set ANSI_NULLS ON 2 set QUOTED_IDENTIFIER ON 3 go 4 5 ALTER function [dbo].[fn_Randtime] 6 ( 7 @begin_date datetime, --開始時間 8 @end_date datetime --結束時間 9 ) 10 returns Dateti...
阅读全文
摘要:1 rowcount的用法: 2 3 rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, 4 set rowcount 10 5 select * from 表A 6 7 这样的查询只会返回表A中的前10条数据。它和 "select top 10 * from 表A" 的作用一样。注意一点,set rowcount 的设置会在整个会话...
阅读全文
摘要:with tabs as ( select ROW_NUMBER() over(partition by customerID order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order ) select MAX(rows) as '下单次数',customerID from tabs group...
阅读全文
摘要:1 ALTER TRIGGER [InsertStoreJITOnloadQuantity] ON [dbo].[Sourceing] 2 After INSERT 3 AS 4 --登記計劃數量(新增時YN=0) 5 Declare @ID int 6 Declare @MaterielCode varchar(50) 7 Declare @Typeofrefno...
阅读全文
摘要:1 ALTER FUNCTION [dbo].[fnt_SplitString] 2 ( 3 @p1 varchar(Max), 4 @p3 varchar(255) 5 ) 6 RETURNS 7 @Table_Var TABLE 8 ( 9 c1 varchar(max) 10 ) 11 AS 12 BEGIN 13 declare @p...
阅读全文

浙公网安备 33010602011771号