SQL procedure 一般编写规则:
/*开头添加procedure说明*/
/*必要注释*/
Tab 键对齐(if,for,while,case when... )
勤看联机帮助
--------------------------------------------
定义临时表变量
DECLARE @myTable1 table (sec_cd char(6), F0010 decimal(18,4), seq int IDENTITY(1, 1), F0020 float)
和实体表一样操作,如:
insert into @myTable1(SEC_CD, F0010)
select sec_cd, F0010 from @myTable
对于临时表 #t
一次执行不可重复creat 和drop ,但是循环一次中可以。
--------------------------------------------
去掉小数查询sql
select f0010 '带四位小数',substring(cast(round(f0010,2) as char),0,charindex('.',f0010)+3) '去掉小数后' from cd_10_ind.dbo.IND_S_QOT_EST
--------------------------------------------
添加链接服务器 2000
exec sp_addlinkedserver 'RMT_SRV','','SQLOLEDB',NULL,NULL,'DRIVER={SQL Server};SERVER=192.168.0.4;UID=sa;PWD=sa;'
exec sp_addlinkedsrvlogin 'RMT_SRV','false'
---2005
EXEC sp_addlinkedserver
@server='CDW_SRV',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='192.168.0.1'
-- Use current login's security context for the link
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'CDW_SRV', @locallogin = NULL , @useself = N'False', @rmtuser = N'Etladmin', @rmtpassword='******'
------------------------------------------------------------------------------------
--为了移动tempdb数据库,运行下面的查询,执行完后重新启动SQLSERVER就可(SQL 2005 选项中不可修改TEMPDB地址)
Alter database tempdb modify file (name = tempdev, filename = 'D:\database\tempdb.mdf')
go
Alter database tempdb modify file (name = templog, filename = 'D:\database\templog.ldf')
Go
--查询阻塞或死锁信息
sp_who_lock