當前位置:首頁 » 存儲配置 » sqlserver存儲過程if

sqlserver存儲過程if

發布時間: 2023-04-12 06:10:30

1. sqlserver如何寫存儲過程

create proc test ------創建存儲過程 test
@a int =『』-----------創建變數 有的存儲過程不需要變數,這個看個人所需要
as ---------------執行以下語句
select * from table where a=@a -------------------後面寫自己需要的語句
go

--------exec test 『1』----------執行存儲過程

2. 在sqlserver的存儲過程中怎樣使用if-else if

跟程序中使用差不多,if,else if,else,只不過在sqlserver中,{}使用begin和end代替,裡面嵌套什麼的,都跟程序中寫的差不多,簡單實例如下:

declare@aint
set@a=1
if@a<0
begin
select'小於0'
end
elseif@a=0
begin
select'等於0'
end
else
begin
if@a>0and@a<1
begin
select'大於0小於1'
end
else
begin
select'大於1'
end
end

在簡單看看程序代碼:
inta=1;

if(a<0)
{
輸出小於0;
}
elseif(a==0)
{
輸出等於0;
}
else
{
if(a>0&&a<1)
{
輸出大於0小於1;
}
else
{
輸出大於1
}
}

3. sqlserver存儲過程違反主鍵約束if語句為什麼語句終止了後面els的print還能輸出求解

違反主鍵約束只是本次if執行不成功了,就會走else,然後調用print列印。

4. 在sqlserver的存儲過程中怎樣使用if-else if

跟程序中使用差不多,if,else
if,else,只不過在sqlserver中,{}使用begin和end代替,裡面嵌套什麼的,都跟程序中寫的差不多,簡單實例如下:
declare
@a
intset
@a=1if
@a<0begin
select
'小於0'endelse
if
@a=0begin
select
'等於0'endelsebegin
if
@a>0
and
@a<1
begin
select
'大於0小於1'
end
else
begin
select
'大於1'
endend在簡單看看程序代碼:int
a=1;if(a<0){
輸出小於0;}else
if(a==0){
輸出等於0;}else{
if(a>0&&a<1)
{
輸出大於0小於1;
}
else
{
輸出大於1
}}

5. 在sqlserver的存儲過程中怎樣使用if-else if

ifa>b
Begin
print'a'
End
Elseifa<b
Begin
print'b'
End
Else
Begin
print'代碼'
End


if-else語法為,最好不要把beginend省略掉。否則,存儲過程比較長可能會報語法錯誤

6. sqlserver怎麼創建存儲過程

1、可視化創建

a.登錄SQL Server

b.打開資料庫==》要創建存儲過程的資料庫==》可編程性==》存儲過程

c.選中「存儲過程」右擊 ,在系出現的對話框中選擇「新建存儲過程」

d.在右側出現的對話框中填寫具體存儲過程內容完成後執行即可

2、代碼創建

a.全手寫代碼

一、定義變數
--簡單賦值
declare@aint
set@a=5
print@a

--使用select語句賦值
declare@user1nvarchar(50)
select@user1='張三'
print@user1
declare@user2nvarchar(50)
select@user2=NamefromST_UserwhereID=1
print@user2

--使用update語句賦值
declare@user3nvarchar(50)
updateST_Userset@user3=NamewhereID=1
print@user3


二、表、臨時表、表變數

--創建臨時表1
createtable#DU_User1
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL
);
--向臨時表1插入一條記錄
insertinto#DU_User1(ID,Oid,[Login],Rtx,Name,[Password],State)values(100,2,'LS','0000','臨時','321','特殊');

--從ST_User查詢數據,填充至新生成的臨時表
select*into#DU_User2fromST_UserwhereID<8

--查詢並聯合兩臨時表
select*from#DU_User2whereID<3unionselect*from#DU_User1

--刪除兩臨時表
droptable#DU_User1
droptable#DU_User2

--創建臨時表
CREATETABLE#t
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL,
)

--將查詢結果集(多條數據)插入臨時表
insertinto#tselect*fromST_User
--不能這樣插入
--select*into#tfromdbo.ST_User

--添加一列,為int型自增長子段
altertable#tadd[myid]intNOTNULLIDENTITY(1,1)
--添加一列,默認填充全球唯一標識
altertable#tadd[myid1](newid())

select*from#t
droptable#t
--給查詢結果集增加自增長列

--無主鍵時:
selectIDENTITY(int,1,1)asID,Name,[Login],[Password]into#tfromST_User
select*from#t

--有主鍵時:
select(selectSUM(1)fromST_UserwhereID<=a.ID)asmyID,*fromST_UseraorderbymyID
--定義表變數
declare@ttable
(
idintnotnull,
msgnvarchar(50)null
)
insertinto@tvalues(1,'1')
insertinto@tvalues(2,'2')
select*from@t
三、循環

--while循環計算1到100的和
declare@aint
declare@sumint
set@a=1
set@sum=0
while@a<=100
begin
set@sum+=@a
set@a+=1
end
print@sum
四、條件語句

--if,else條件分支
if(1+1=2)
begin
print'對'
end
else
begin
print'錯'
end

--whenthen條件分支
declare@todayint
declare@weeknvarchar(3)
set@today=3
set@week=case
when@today=1then'星期一'
when@today=2then'星期二'
when@today=3then'星期三'
when@today=4then'星期四'
when@today=5then'星期五'
when@today=6then'星期六'
when@today=7then'星期日'
else'值錯誤'
end
print@week


五、游標

declare@IDint
declare@Oidint
declare@Loginvarchar(50)

--定義一個游標
declareuser_curcursorforselectID,Oid,[Login]fromST_User
--打開游標
openuser_cur
while@@fetch_status=0
begin
--讀取游標
fetchnextfromuser_curinto@ID,@Oid,@Login
print@ID
--print@Login
end
closeuser_cur
--摧毀游標
deallocateuser_cur
六、觸發器

觸發器中的臨時表:

Inserted
存放進行insert和update操作後的數據
Deleted
存放進行delete和update操作前的數據

--創建觸發器
CreatetriggerUser_OnUpdate
OnST_User
forUpdate
As
declare@msgnvarchar(50)
--@msg記錄修改情況
select@msg=N'姓名從「'+Deleted.Name+N'」修改為「'+Inserted.Name+'」'fromInserted,Deleted
--插入日誌表
insertinto[LOG](MSG)values(@msg)

--刪除觸發器
droptriggerUser_OnUpdate
七、存儲過程

--創建帶output參數的存儲過程
CREATEPROCEDUREPR_Sum
@aint,
@bint,
@sumintoutput
AS
BEGIN
set@sum=@a+@b
END

--創建Return返回值存儲過程
CREATEPROCEDUREPR_Sum2
@aint,
@bint
AS
BEGIN
Return@a+@b
END

--執行存儲過程獲取output型返回值
declare@mysumint
executePR_Sum1,2,@mysumoutput
print@mysum

--執行存儲過程獲取Return型返回值
declare@mysum2int
execute@mysum2=PR_Sum21,2
print@mysum2八、自定義函數

函數的分類:

1)標量值函數

2)表值函數

a:內聯表值函數

b:多語句表值函數

3)系統函數--新建標量值函數
createfunctionFUNC_Sum1
(
@aint,
@bint
)
returnsint
as
begin
return@a+@b
end

--新建內聯表值函數
createfunctionFUNC_UserTab_1
(
@myIdint
)
returnstable
as
return(select*fromST_UserwhereID<@myId)

--新建多語句表值函數
createfunctionFUNC_UserTab_2
(
@myIdint
)
returns@ttable
(
[ID][int]NOTNULL,
[Oid][int]NOTNULL,
[Login][nvarchar](50)NOTNULL,
[Rtx][nvarchar](4)NOTNULL,
[Name][nvarchar](5)NOTNULL,
[Password][nvarchar](max)NULL,
[State][nvarchar](8)NOTNULL
)
as
begin
insertinto@tselect*fromST_UserwhereID<@myId
return
end

--調用表值函數
select*fromdbo.FUNC_UserTab_1(15)
--調用標量值函數
declare@sint
set@s=dbo.FUNC_Sum1(100,50)
print@s

--刪除標量值函數
dropfunctionFUNC_Sum1
談談自定義函數與存儲過程的區別:

一、自定義函數:

1.可以返回表變數

2.限制頗多,包括

不能使用output參數;

不能用臨時表;

函數內部的操作不能影響到外部環境;

不能通過select返回結果集;

不能update,delete,資料庫表;

3.必須return一個標量值或表變數

自定義函數一般用在復用度高,功能簡單單一,爭對性強的地方。

二、存儲過程

1.不能返回表變數

2.限制少,可以執行對資料庫表的操作,可以返回數據集

3.可以return一個標量值,也可以省略return

存儲過程一般用在實現復雜的功能,數據操縱方面。

7. SQL Server的存儲過程怎麼寫

SQL server中如何存儲:

首先准備數據,測試存儲過程

use ssqadm;

創建測試books表

create table books_test ( book_id int identity(1,1) primary key,

book_name varchar(20),book_price float,book_auth varchar(10));

插入測試數據

insert into books_test (book_name,book_price,book_auth)values

('論語',25.6,'孔子'),

('天龍八部',25.6,'金庸'),

('雪山飛狐',32.7,'金庸'),

('平凡的世界',35.8,'路遙'),

('史記',54.8,'司馬遷');

select * from books_test;*/

創建無參存儲過程

if (exists (select * from sys.objects where name = 'getAllBooks'))

drop proc getAllBooks

go

create procere getAllBooks

as

begin

select * from books_test;

調用,執行存儲過程

exec getAllBooks;

end

go

修改存儲過程

alter procere getallbooks

as

select book_name from books_test;

修改存儲過程的名稱

sp_rename getallbooks,proc_get_allbooks;

go

exec proc_get_allbooks;

go

創建帶參數的存儲過程

use ssqadm

go

if (exists (select * from sys.objects where name = 'searchbooks'))

drop proc searchbooks

exec searchbooks

執行存儲searchbooks得到如下結果:

go

create procere searchbooks (@bookid int)--括弧裡面是

as

begin

declare @book_id int;定義一個標量變數,只是保證存儲過程的完整性,在本存儲是多此一舉的。

set @book_id = @bookid;

select* from books_test where book_id = @book_id;

end;

go

-- exec searchbooks

執行存儲searchbooks得到如下結果:

創建帶兩個參數的存儲過程

use ssqadm

go

if (exists (select * from sys.objects where name = 'book_test2'))

drop proc book_test2

exec book_test2

執行存儲book_test2得到如下結果:

go

create procere book_test2

(@bookid int,@bookname varchar(20))括弧裡面是

as

begin

declare @book_id int;

定義一個標量變數,只是保證存儲過程的完整性,在本存儲是多此一舉的。

declare @book_name varchar(20);

set @book_id = @bookid;

set @book_name = @bookname;

select* from books_test where book_id =

@book_id and book_name = @book_name;

end;

go

exec book_test2

(7)sqlserver存儲過程if擴展閱讀:

SQL Server中查詢存儲命令子句:

USE [SSQADM]

Use 是跳轉到哪個資料庫,對這個資料庫進行操作。

GO

GO向 SQL Server 實用工具發出一批 Transact-SQL 語句結束的信號,相當於提交上面的SQL語句。

GO是把t-sql語句分批次執行

(一步成功了才會執行下一步,即一步一個GO)

/****** Object: StoredProcere [dbo].[PROC_four_five_hr]

Script Date: 07/30/2018 13:44:55 ******/

SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ON

8. 請教ms sqlserver存儲過程如何寫多個if語句

select @user_blog_lock='yes'elseselect @user_blog_lock='no'GO這樣寫,後面那二條語句是沒有執行到的。用到else if 是錯誤的語法,如果都都用if,第二條語句以後都不能執行了
變數的輸入是對的。
第二、三條IF語句是不能執行了。看聯機叢書 F1
-----------------------IF...ELSE在執行 Transact-SQL 語句時強加條件。如果條件滿足(布爾表達式返回 TRUE 時),則在 IF 關鍵字及其條件之後執行 Transact-SQL 語句。可選的 ELSE 關鍵字引入備用的 Transact-SQL 語句,當不滿足 IF 條件時(布爾表達式返回 FALSE),就執行這個語句。
語法IF Boolean_expression
{ sql_statement | statement_block }[ ELSE{ sql_statement | statement_block } ]參數Boolean_expression
是返回 TRUE 或 FALSE 的表達式。如果布爾表達式中含有 SELECT 語句,必須用圓括弧將 SELECT 語句括起來。
{sql_statement | statement_block}
Transact-SQL 語句或用語句塊定義的語句分組。除非使用語句塊,否則 IF 或 ELSE 條件只能影響一個 Transact-SQL 語句的性能。若要定義語句塊,請使用控制流關鍵字 BEGIN 和 END。如果在 IF...ELSE 塊的 IF 區和 ELSE 區都使用了 CREATE TABLE 語句或 SELECT INTO 語句,那麼 CREATE TABLE 語句或 SELECT INTO 語句必須指向是相同的表名。
注釋IF...ELSE 結構可以用在批處理中,存儲過程中(經常使用這種結構測試是否存在著某個參數),以及特殊查詢中。
可以在其它 IF 之後或在 ELSE 下面,嵌套另一個 IF 測試。對於嵌套層數沒有限制。
示例
A. 使用一個 IF...ELSE 塊
下面的示例顯示帶有語句塊的 IF 條件。如果書的平均價格不低於 $15,那麼就顯示文本:Average title price is more than $15.
USE pubsIF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15BEGINPRINT 'The following titles are excellent mod_cook books:'PRINT ' 'SELECT SUBSTRING(title, 1, 35) AS Title
FROM titles
WHERE type = 'mod_cook'ENDELSEPRINT 'Average title price is more than $15.'
下面是結果集:
The following titles are excellent mod_cook books:Title-----------------------------------
Silicon Valley Gastronomic Treats
The Gourmet Microwave
(2 row(s) affected)
B. 使用多個 IF...ELSE 塊
下面的示例使用了兩個 IF 塊。如果書的平均價格不低於 $15,那麼就顯示文本:Average title price is more than $15。如果現代烹調書的平均價格高於 $15,則顯示現代烹調書價格昂貴的語句。
USE pubsIF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') < $15BEGINPRINT 'The following titles are excellent mod_cook books:'PRINT ' 'SELECT SUBSTRING(title, 1, 35) AS Title
FROM titles
WHERE type = 'mod_cook'ENDELSEIF (SELECT AVG(price) FROM titles WHERE type = 'mod_cook') > $15BEGINPRINT 'The following titles are expensive mod_cook books:'PRINT ' 'SELECT SUBSTRING(title, 1, 35) AS Title

9. 在sqlserver的存儲過程中怎樣使用if-else if

if a>b
Begin
print 'a'
End
Else if a<b
Begin
print 'b'
End
Else
Begin
print '代碼'
End

熱點內容
查詢重復欄位的sql語句 發布:2025-02-13 03:12:42 瀏覽:322
8uftp上傳網站 發布:2025-02-13 03:01:57 瀏覽:242
電腦玩游戲如何配置電源 發布:2025-02-13 03:01:53 瀏覽:361
微信怎麼上傳頭像不了 發布:2025-02-13 02:57:04 瀏覽:118
c語言矩陣的轉置 發布:2025-02-13 02:38:43 瀏覽:624
rowphp 發布:2025-02-13 02:37:16 瀏覽:711
光遇安卓服周年傘在哪裡領取 發布:2025-02-13 02:22:18 瀏覽:674
寫mv腳本軟體 發布:2025-02-13 02:21:56 瀏覽:696
超內核源碼 發布:2025-02-13 02:12:54 瀏覽:444
趣粉腳本 發布:2025-02-13 02:11:23 瀏覽:952