SQL Server 中的存儲過程(Procere),帶入參數和出參數。
存儲過程(Procere)-基本創建與操作。
--一、無參存儲過程
create procere PTitles
as
select * from titles
go
--2,執行存儲過程
execute PTitles
go
--3,移除存儲過程
--drop procere PTitles
go
5.存儲過程(Procere)-帶入參。
create proc P_Titles_ByType
@type char(12) --入參
as
select * from titles where type=@type
go
--,執行帶參數的存儲過程
--a)方式一
exec P_Titles_ByType @type='business'
go
--b)方式二
exec P_Titles_ByType 'business'
6.存儲過程(Procere)-帶入參和出參。
create proc P_Titles_ByTypeAndPrice
@type char(12), --入參
@price money --入參
as begin
select * from titles
where type=@type and price>@price
end
B. 如何在sqlserver存儲過程中輸出參數,語句是什麼,我不用輸出參數,我只是在體內輸出語句,請問是什麼
在定義時定義一個ouput參數,如以下存儲過程根據時間產生一個唯一ID
CREATE PROCEDURE [getid](@id char(17) OUTPUT)--產生唯一碼
AS
DECLARE @a datetime
select @a=getdate()
DECLARE @time1 char(10)
DECLARE @time2 char(10)
DECLARE @time3 char(10)
DECLARE @time4 char(10)
DECLARE @time5 char(10)
DECLARE @time6 char(10)
DECLARE @time7 char(10)
Select @time1=str(Datename(year,@a))
Select @time2=str(Datename(month,@a))
if Datename(month,@a)<10 select @time2='0'+rtrim(ltrim(@time2))
Select @time3=str(Datename(day,@a))
if Datename(day,@a)<10 select @time3='0'+rtrim(ltrim(@time3))
select @time4=str(Datename(hour,@a))
if Datename(hour,@a)<10 select @time4='0'+rtrim(ltrim(@time4))
Select @time5=str(Datename(minute,@a))
if Datename(minute,@a)<10 select @time5='0'+rtrim(ltrim(@time5))
Select @time6=str(Datename(second,@a))
if Datename(second,@a)<10 select @time6='0'+rtrim(ltrim(@time6))
Select @time7=str(Datename(Millisecond,@a))
if Datename(Millisecond,@a)<10 select @time7='0'+rtrim(ltrim(@time7))
if Datename(Millisecond,@a)<100 select @time7='0'+rtrim(ltrim(@time7))
select @id=ltrim(rtrim(@time1))+ltrim(rtrim(@time2))+ltrim(rtrim(@time3))+ltrim(rtrim(@time4))+ltrim(rtrim(@time5))+ltrim(rtrim(@time6))+ltrim(rtrim(@time7))
GO
在其它存儲過程中用下例語句調用以上這個存儲過程,如下
DECLARE @id char(17)
EXEC [getid] @id OUTPUT
這樣@id就可以得到getid的返回值了
C. 帶參數的sqlserver存儲過程的定義
@是標示符,代表是變數
一個@表示局部變數
兩個@表示是全局變數,全局變數是有伺服器定義的
D. SqlServer 創建表命令中的參數——TEXTIMAGE_ON
在使用SqlServer創建表時會遇到一些參數,比如PAD_INDEX=OFF,TEXTIMAGE_ON等等,這里把這些參數的含義做一個小結,在使用時避免出錯。下面是創建表的腳本:
首先說一下TEXTIMAGE_ON [PRIMARY],這個表中有一個大數據欄位[Json] nvarchar ,TEXTIMAGE_ON 的作用是將列存儲在指定文件組中,如果沒有指定 TEXTIMAGE_ON,則大數據列將與表存儲在同一文件組中。如果表中沒有大數據欄位,使用這個參數會報錯:
E. SQLSERVER EXEC參數的問題
其實就是對同名稱的存儲過程加一個區分,但是又可以使用同一個名稱來刪除。
形象的說就是一個存儲過程有兩個版本。