當前位置:首頁 » 編程語言 » sqlserverin參數

sqlserverin參數

發布時間: 2023-01-09 06:10:51

A. sqlServer跨資料庫執行存儲,並且這個存儲帶有表類型參數,需要怎麼聲明傳入

參數定義
單個參數
1> CREATE PROCEDURE HelloWorld1
2> @UserName VARCHAR(10)
3> AS
4> BEGIN
5> PRINT 'Hello' + @UserName + '!';
6> END;
7> go
1> DECLARE @RC int;
2> EXECUTE @RC = HelloWorld1 'Edward' ;
3> PRINT @RC;
4> go
HelloEdward!
0

IN、OUT、IN OUT
註:
SQL Server 的 OUTPUT 需要寫在變數數據類型後面。

SQL Server 沒有 IN OUT 關鍵字
OUTPUT 已經相當於 IN OUT 了。

1> CREATE PROCEDURE HelloWorld2
2> @UserName VARCHAR(10),
3> @OutVal VARCHAR(10) OUTPUT,
4> @InoutVal VARCHAR(10) OUTPUT
5> AS
6> BEGIN
7> PRINT 'Hello ' + @UserName + @InoutVal + '!';
8> SET @OutVal = 'A';
9> SET @InoutVal = 'B';
10> END;
11> go

1>
2> DECLARE @RC int, @OutVal VARCHAR(10), @InoutVal VARCHAR(10);
3> BEGIN
4> SET @InoutVal = '~Hi~';
5> EXECUTE @RC = HelloWorld2 'Edward', @OutVal OUTPUT, @InoutVal OUTPUT;
6> PRINT @RC;
7> PRINT '@OutVal=' + @OutVal;
8> PRINT '@InoutVal=' + @InoutVal;
9> END
10> go
Hello Edward~Hi~!
0
@OutVal=A
@InoutVal=B

參數的默認值
1> CREATE PROCEDURE HelloWorld3
2> @UserName VARCHAR(10),
3> @Val1 VARCHAR(20) = ' Good Moning,',
4> @Val2 VARCHAR(20) = ' Nice to Meet you'
5> AS
6> BEGIN
7> PRINT 'Hello ' + @UserName + @Val1 + @Val2 + '!';
8> END;
9> go
1>
2> DECLARE @RC int;
3> BEGIN
4> EXECUTE @RC = HelloWorld3 'Edward';
5> PRINT @RC;
6> EXECUTE @RC = HelloWorld3 'Edward', ' Good Night,';
7> PRINT @RC;
8> EXECUTE @RC = HelloWorld3 'Edward', ' Good Night,', ' Bye';
9> PRINT @RC;
10> END
11> go
Hello Edward Good Moning, Nice to Meet you!
0
Hello Edward Good Night, Nice to Meet you!
0
Hello Edward Good Night, Bye!
0

指定參數名稱調用
此部分使用 「參數默認值」那一小節的存儲過程。
用於說明當最後2個參數是有默認的時候,如何跳過中間那個。
1> DECLARE @RC int;
2> BEGIN
3> EXECUTE @RC = HelloWorld3 'Edward';
4> PRINT @RC;
5> EXECUTE @RC = HelloWorld3 'Edward', @Val1=' Good Night,';
6> PRINT @RC;
7> EXECUTE @RC = HelloWorld3 'Edward', @Val1=' Good Night,', @Val2=' Bye';
8> PRINT @RC;
9> EXECUTE @RC = HelloWorld3 'Edward', @Val2=' HeiHei ';
10> PRINT @RC;
11> END
12> go
Hello Edward Good Moning, Nice to Meet you!
0
Hello Edward Good Night, Nice to Meet you!
0
Hello Edward Good Night, Bye!
0
Hello Edward Good Moning, HeiHei !
0

B. 關於sqlserver中存儲過程裡面in關鍵字的使用

首先就是@chooseid的問題
如果你這樣in的話
即使你的 @chooseid=1,2,3
他也會把這幾個看做一個整體
也就是把 id 同 '1,2,3'這個整體去比對
而不是拆開

這樣的話只能用動態去構建

declare @sql varchar(1000)
set @sql=''
select @sql='delete from table1 where id in ('+ @chooseid+') and uid='+@uid
exec(@sql)

動態構建 也是可以增加@uid的條件的啊...
不太明白你的意思
你可以把代碼寫全
肯定是可以動態的
寫全了 我給你寫

C. 帶參數的sqlserver存儲過程的定義

@是標示符,代表是變數
一個@表示局部變數
兩個@表示是全局變數,全局變數是有伺服器定義的

D. sqlserver中想用in判斷范圍,但只能輸入一個字元串參數,如何拆字元串然後動態拼in()裡面的內容呢

字元串應該類似'a,b,c,d' 或者'1,2,3,4'這樣的吧?
拼字元串 set @str='select * from tb where 欄位 in ('+char(39)+replace('字元串',',',char(39)+','+char(39)))+char(39)+')'
exec(@str)
就是把'a,b,c,d' 替換成 ('a','b','c','d')

E. sqlserver存儲過程如何建立可選參數

  1. SQL Server 中的存儲過程(Procere),帶入參數和出參數。

  2. 存儲過程(Procere)-基本創建與操作。

  3. --一、無參存儲過程

  4. 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

F. JDBC連接sqlserver資料庫in()參數

你好!
不是你拼接的問題吧我覺得你那個 SQL 在SQLSERVER 客戶端就不能執行!
你先把SQL 在客戶端正常執行之後,在放入程序哇!
您試試吧!
歡迎追問,謝謝採納~~

G. mssql2008中,in參數的個數上限是多少。像這樣select * from A where A.Num in(1,2,3...)

沒上限的吧, IN後面也可以跟 (select num from B),這樣的話就是B表裡有多少數據就可以查多少

H. sqlserver2008 in的參數傳值批量查詢

select * from t_user where name in (@name) 這種叫參數化,參數化會被認定為一個字元而不是多個
如果不計效率的話,可以改寫為:select * from t_user where ','+@name+',' like '%,'+name+',%'

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:677
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:250
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:799
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705