惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 站在天空下的猪

SqlBulkCopy批量转移数据备注 根据文本框联动下拉框(jquery 插件) - 站在天空下的猪 - 博客园 关于用FOMR提交编码的问题 jquery 图片预览插件 - 站在天空下的猪 - 博客园 自己写的一个jquery模板引擎(json比较好用) Document 对象的常用方法 今天发现梅花雨日历控件蛮好用的哟,腾讯都在用 身份证对应县及县的行政区划代码 终于解决了一个AjaxPro无刷新的问题了,可以引用在短信方面滴asp.net 2.0 昨天很失败 一个简单的FileUpload处理逻辑 C# 判断是否为数字 DataBinder.Eval总结 asp.net 2.0中实现防盗链 如何把HTML语句直接保存到数据库 如何取得IP/用户名等信息 “ConnectionString 属性尚未初始化”的另类解决办法 asp.net常用函数表 C#中计算两个时间的差
【SQLSERVER】存储过程基础
站在天空下的猪 · 2007-05-29 · via 博客园 - 站在天空下的猪

1.声明变量

DECLARE @F001 SMALLINT,
   
          @F002 INTEGER,
   
          @F003 VARCHAR(20),
             
@F004 CHAR(20),
   
          @F002 MONEY

2.赋值语句

set @F001 = space(40)

3.条件判断(IF...ELSE)

If condition Begin
    
[ statements  ]
END
ELSE BEGIN
    
[ elseifstatements ]
END

4.多分支判断(case...when...then...else...end)

SET @F011 =
CASE
       
WHEN [testexpression1] THEN  @F001
       
WHEN [testexpression2] THEN  @F002
       
WHEN [testexpression3] THEN  @F003
       
WHEN [testexpression4] THEN  @F004
END

5.循环(while)

While condition Begin
   
[ statements ]
End

6.动态定义游标

SET @strSQL = ' DECLARE  name_cursor  CURSOR  FOR  ' + @inSQL
EXEC  (@strSQL)

7.遍历游标

FETCH NEXT FROM name_cursor into @F001,@F002
WHILE  @@FETCH_STATUS = 0 BEGINFETCH NEXT FROM name_cursor into @F001,@F002END

说明:FETCH_STATUS检索到数据返回0,失败返回-1,可判断是否滚动未到结尾。

8.获得游标行数

SET @RECCNT = @@ROWCOUNT

9.事务处理

BEGIN distributed transactionWHILE @@TRANCOUNT > 0
       
commit transaction

10.字符串连接

SET @m_sql = @m_sql + ' Where F001 = ''' + @F001 + ''''
SET @m_sql = @m_sql + ' F002 = ' + CONVERT(varchar,@F002)

11.创建临时表存储外部数据表
说明:临时过程用 # 和 ## 命名,可以由任何用户创建。创建过程后,局部过程的所有者是唯一可以使用该过程的用户。

CREATE TABLE #DMPARHED
(FMCD   
int,
FMNAM  
varchar(50),
MGYO1  
smallint,
constraint DMPARHED_P primary key (FMCD))
SET @aSQL = ''
SET @aSQL = @aSQL + 'INSERT INTO #DMPARHED'
SET @aSQL = @aSQL + ' SELECT FMCD,FMNAM,MGYO1 FROM'
SET @aSQL = @aSQL + ' OPENQUERY(Lk_MDB_NEO32, ''SELECT FMCD,FMNAM,MGYO1 FROM DMPARHED'
SET @aSQL = @aSQL + ' WHERE SYSNO = 1'')'
execute(@aSQL)

创建临时表的另类方法:

select a.name,a.password from
with
as temp1
select * from emp
(
select * from temp1
union
select * from temp1) a
where a.name='hao'

12.存储过程的调用及返回值
(1)存储过程的声明

CREATE PROCEDURE name_produce
    
@F001  VARCHAR(20),
           
@F002  SMALLINT OUTPUT

(2)VB.NET调用存储过程

Private SqlCmd As New OleDb.OleDbCommand

SqlCmd.CommandText 

= "prNK3020SC03"
SqlCmd.CommandType 
= CommandType.StoredProcedureDim parampre1 As OleDb.OleDbParameter = SqlCmd.Parameters.Add( _
         
New OleDb.OleDbParameter("@F001", OleDb.OleDbType.VarChar, 20, _
         ParameterDirection.Input))
Dim parampre2 As OleDb.OleDbParameter = SqlCmd.Parameters.Add( _
        
New OleDb.OleDbParameter("@F002", OleDb.OleDbType.SmallInt))
parampre2.Direction 
= ParameterDirection.Output

SqlCmd.Parameters(

"@F001").Value = aF001
SqlCmd.Parameters(
"@F002").Value = aF002
SqlCmd.ExecuteNonQuery()
aF002 
= SqlCmd.Parameters("@F002").Value.ToString()

(3)存储过程调用存储过程

DECLARE @C001       VARCHAR(20),
             
@C002       SMALLINT
EXEC name_produce @C001,@C002 output

CREATE PROCEDURE dbo.getUserName
@UserID int,
@UserName varchar(40) output
as
set nocount on
begin
if @UserID is null return
select @UserName=username
from dbo.[userinfo]
where userid=@UserID
return
end

13.Update语句常见错误总结

--
Update name_table set
       F001 
= @F181,
       F002 
= @F182
Where
       F003 
= @F003
--×
Update name_table
       F001 
= @F181,
       F002 
= @F182
Where
       F003 
= @F003
--×
Update name_table set
       F001 
= @F181,
       F002 
= @F182,
Where
       F003 
= @F003
--×
Update name_table set
       ,F001 
= @F181
       ,F002 
= @F182
Where
       F003 
= @F003

14.Insert语句常见语法错误总结

--
INSERT INTO name_table(
,KEY_FIELD,BUSYOCD
)
Values(
@F001,@F002
)
--×
INSERT INTO name_table(
F001,F002
)
Values(
,
@F001,@F002
)
--×
INSERT INTO name_table(
F001,F002
)
Values(
@F001,@F002,
)
--×
INSERT  name_table(
F001,F002
)
Values(
@F001,@F002
)