随笔分类 - SQL MSSQL
摘要:表名前使用一个#号(#),临时表是局部的,只允许当前会话使用;使用两个#号(##),临时表是全局的,当前在线的会话都可以使用,在断开连接后sql会自动删除临时表。1 create table #a2 (3 id int,4 name varchar(50)5 )6 insert into #a(id,name) values(1,'123')7 select * from #a8 drop table #a临时表除了名称前多了#号外,其他操作与普通表完全一样。tb_Student是已建立好的表,我们通过临时表temp把tb_Student表中的内容复制到tb_lizi表中,可以
阅读全文
摘要:1、语法李和鑫。1 BEGIN TRY2 { sql_statement | statement_block }3 END TRY4 BEGIN CATCH5 [ { sql_statement | statement_block } ]6 END CATCH2、错误检索信息。在 CATCH 块的作用域内,可以使用以下系统函数来获取导致 CATCH 块执行的错误消息:ERROR_NUMBER() 返回错误号。ERROR_SEVERITY() 返回严重性。ERROR_STATE() 返回错误状态号。ERROR_PROCEDURE() 返回出现错误的存储过程或触发器的名称。ERROR_LINE..
阅读全文
摘要:1 --BeginTrans:开始事务 2 --CommitTrans:提交事务 3 --RollBackTrans:回滚事务 4 --事务具有原子性,要么不执行,要么全执行,一旦成功执行永久保存。下面是个转账的事务应用的例子。 5 If Not Exists (Select * From sysobjects Where name = 'bank') 6 Begin 7 Create Table bank 8 ( 9 bankid int identity(1,1) Primary Key ,10 username varchar(50) not null,11 rmbn..
阅读全文
摘要:网上很多人在讨论In和Exists的性能对比,本人也搞不懂哪个性能更佳,只是一般在小表中用In,而在大表中用Exists。下面只是举例如果使用,以勉被喷了。1、In。1 select * from table1 a2 where a.Id in (select Id from table2)3 4 select * from table1 a5 where a.Id not in (select Id from table2)2、Exists。 1 select * from QVS_CUSTPACKINGSLIPPRINTJOUR a 2 where exists 3 (select PR.
阅读全文
摘要:1、内联接(inner join)。内联接,显示连接表都有的记录,并按最多记录子表显示最终结果。1 select * from table1 a2 inner join table2 b on a.Id = b.Id3 where a.field...2、外联接(outer join)。外联接,又分为左联接(left join)和右联接(right join),显示主表所有记录,而子没有相关记录部分返回‘NULL’。outer join默认为左联接。1 select * from table1 a2 left join table2 b on a.Id = b.Id --显示table1的..
阅读全文
摘要:1、系统自带储存过程。 1 exec sp_databases; --查看数据库 2 exec sp_tables; --查看表 3 exec sp_columns student;--查看列 4 exec sp_helpIndex student;--查看索引 5 exec sp_helpConstraint student;--约束 6 exec sp_stored_procedures; 7 exec sp_helptext 'sp_stored_procedures';--查看存储过程创建、定义语句 8 exec sp_rename student, stuInfo;-
阅读全文
摘要:一个表有三个字段id,dt,d分别存放id,时间,数值id dt d12004-08-11 12:12:00.00092 2005-09-11 12:08:00.00023 2005-08-11 12:12:00.00064 2005-09-11 12:12:00.000105 2005-08-11 12:12:00.0000要求按照时间里的月份分组求d字段和 1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[abc]') and OBJECTPROPERTY(id, N'Is
阅读全文
摘要:在很多ERP中进行表循环,是非常简单的。如QAD中用For each table就可以循环表,AX中用While select table。 在MSSQL中,可以通过两种方式进行表循环。1、通过游标。 1 declare @result table 2 ( 3 custid int, 4 ordermonth datetime, 5 qty int, 6 runqty int, 7 primary key(custid,ordermonth) 8 ); 9 10 declare 11 @custid as int,12 @prvcustid as int,13 @or...
阅读全文

浙公网安备 33010602011771号