sqlserver里調用存儲過程的具體操作步驟如下:
網站建設哪家好,找成都創新互聯公司!專注于網頁設計、網站建設、微信開發、重慶小程序開發、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了安慶免費建站歡迎大家使用!
1、打開SQL Server Managment管理工具,新建一個表。
2、然后在表中插入一些樣例數據。
3、接下來在SQL Server Managment中右鍵單擊可編程性,選擇新建存儲過程。
4、然后在SQL編寫界面中編寫SQL語句,注意這里的@name就是接收的輸入參數。
5、編寫好存儲過程,執行一下,就會在可編程性下面找到創建的存儲過程。
6、緊接著,會彈出一個【執行過程】的界面,里面有存儲過程的參數,在【值】這一列輸入想要傳入的參數值,比如10,然后點擊【確定】按鈕,就可以看到執行結果100了。
問題不是很困難!說說思路吧!具體實現希望你自己能實現.
1.疑惑:
select ObjectID from PAPlanSubject where ObjectID in(
select ObjectID from PAPlanObject where PlanID=8 and DeptPersonNo in(
select CAST(PersonID as varchar) from SPerson where
PersonName='蔡顯林'))
即然只返回一個ObjectID干嘛要用IN去操作,肯定是一對一的操作,等于即可.
2.思路:
1.實現傳入多姓名查詢出對應多PersonID,傳入形成這樣條件:('張婧媛','羅忠強','xxx','xxx')形成Sql條件 PersonName in('張婧媛','羅忠強','xxx','xxx') 即可
2.第一點與第二點可以封裝一個sql語句實現列-PersonID,ObjectID多條語句,如select PersonID,ObjectID from 表與表之間的關聯
3.形成這樣效果即可添加成功
insert into PAPlanSubject(PlanID,KindID,ObjectID,PersonID,NullAble,Flag,Valid,State)
select 8,25,ObjectID,PersonID,0,0,0,0 from XXX表
同時運行上面這條SQL,這樣就是批量添加PAPlanSubject表中.
希望能幫到你!
有問題可以追問我當及時回答!
代碼是最好的文字,不多說,請看我的代碼,并給分,呵呵。
--step1. 建表
if exists(select * from sysobjects where id=object_id('student') and objectproperty(id,'IsTable')=1)
drop table student
go
create table student
(
id int identity(100,10) not null
,sname varchar(10) not null
,sno varchar(30) not null
)
go
--step2.建存儲過程
if exists(select * from sysobjects where id=object_id('proc_demo') and objectproperty(id,'IsProcedure')=1)
drop procedure proc_demo
go
create procedure proc_demo
@o_maxid int output
as
set nocount on
--如果希望大小寫敏感,使用第一句,因為SQL Server默認是大小寫不敏感的
--update student set sno='cay_'+sno where ascii(substring(sname,1,1))=87 and ascii(substring(sname,2,1))=65 and sno not like 'cay_%'
update student set sno='cay_'+sno where sname like 'WA%' and sno not like 'cay_%'
print convert(varchar(10),@@rowcount)+'條記錄符合條件并被處理'
select @o_maxid=max(id) from student where id=100
if(@o_maxid is null) print '沒有找到符合條件的最大記錄'
set nocount off
go
--測試數據1
truncate table student
set identity_insert student on
insert into student(id,sname,sno)values(1,'WA1','1');
insert into student(id,sname,sno)values(2,'wa2','2');
insert into student(id,sname,sno)values(3,'3','3');
set identity_insert student off
go
--測試數據2
truncate table student
insert into student(sname,sno)values('WA1','1');
insert into student(sname,sno)values('wa2','2');
insert into student(sname,sno)values('3','3');
go
--測試過程
declare @maxid int
exec proc_demo @maxid out
print '最大id是'+convert(varchar(10),@maxid)
go
1、可視化創建
a.登錄SQL Server
b.打開數據庫==》要創建存儲過程的數據庫==》可編程性==》存儲過程
c.選中“存儲過程”右擊 ,在系出現的對話框中選擇“新建存儲過程”
d.在右側出現的對話框中填寫具體存儲過程內容完成后執行即可
2、代碼創建
a.全手寫代碼
一、定義變量
--簡單賦值?
declare?@a?int
set?@a=5?
print?@a?
--使用select語句賦值?
declare?@user1?nvarchar(50)?
select?@user1='張三'
print?@user1?
declare?@user2?nvarchar(50)?
select?@user2?=?Name?from?ST_User?where?ID=1?
print?@user2?
--使用update語句賦值?
declare?@user3?nvarchar(50)?
update?ST_User?set?@user3?=?Name?where?ID=1?
print?@user3
二、表、臨時表、表變量
--創建臨時表1?
create?table?#DU_User1?
(?
[ID]?[int]??NOT?NULL,?
[Oid]?[int]?NOT?NULL,?
[Login]?[nvarchar](50)?NOT?NULL,?
[Rtx]?[nvarchar](4)?NOT?NULL,?
[Name]?[nvarchar](5)?NOT?NULL,?
[Password]?[nvarchar](max)?NULL,?
[State]?[nvarchar](8)?NOT?NULL
);?
--向臨時表1插入一條記錄?
insert?into?#DU_User1?(ID,Oid,[Login],Rtx,Name,[Password],State)?values?(100,2,'LS','0000','臨時','321','特殊');?
--從ST_User查詢數據,填充至新生成的臨時表?
select?*?into?#DU_User2?from?ST_User?where?ID8?
--查詢并聯合兩臨時表?
select?*?from?#DU_User2?where?ID3?union?select?*?from?#DU_User1?
--刪除兩臨時表?
drop?table?#DU_User1?
drop?table?#DU_User2
--創建臨時表?
CREATE?TABLE?#t?
(?
[ID]?[int]?NOT?NULL,?
[Oid]?[int]?NOT?NULL,?
[Login]?[nvarchar](50)?NOT?NULL,?
[Rtx]?[nvarchar](4)?NOT?NULL,?
[Name]?[nvarchar](5)?NOT?NULL,?
[Password]?[nvarchar](max)?NULL,?
[State]?[nvarchar](8)?NOT?NULL,?
)?
--將查詢結果集(多條數據)插入臨時表?
insert?into?#t?select?*?from?ST_User?
--不能這樣插入?
--select?*?into?#t?from?dbo.ST_User?
--添加一列,為int型自增長子段?
alter?table?#t?add?[myid]?int?NOT?NULL?IDENTITY(1,1)?
--添加一列,默認填充全球唯一標識?
alter?table?#t?add?[myid1]?uniqueidentifier?NOT?NULL?default(newid())?
select?*?from?#t?
drop?table?#t
--給查詢結果集增加自增長列?
--無主鍵時:?
select?IDENTITY(int,1,1)as?ID,?Name,[Login],[Password]?into?#t?from?ST_User?
select?*?from?#t?
--有主鍵時:?
select?(select?SUM(1)?from?ST_User?where?ID=?a.ID)?as?myID,*?from?ST_User?a?order?by?myID
--定義表變量?
declare?@t?table
(?
id?int?not?null,?
msg?nvarchar(50)?null
)?
insert?into?@t?values(1,'1')?
insert?into?@t?values(2,'2')?
select?*?from?@t
三、循環
--while循環計算1到100的和?
declare?@a?int
declare?@sum?int
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
--when?then條件分支?
declare?@today?int
declare?@week?nvarchar(3)?
set?@today=3?
set?@week=case
when?@today=1?then?'星期一'
when?@today=2?then?'星期二'
when?@today=3?then?'星期三'
when?@today=4?then?'星期四'
when?@today=5?then?'星期五'
when?@today=6?then?'星期六'
when?@today=7?then?'星期日'
else?'值錯誤'
end
print?@week
五、游標
declare?@ID?int
declare?@Oid?int
declare?@Login?varchar(50)?
--定義一個游標?
declare?user_cur?cursor?for?select?ID,Oid,[Login]?from?ST_User?
--打開游標?
open?user_cur?
while?@@fetch_status=0?
begin
--讀取游標?
fetch?next?from?user_cur?into?@ID,@Oid,@Login?
print?@ID?
--print?@Login?
end
close?user_cur?
--摧毀游標?
deallocate?user_cur
六、觸發器
觸發器中的臨時表:
Inserted?
存放進行insert和update?操作后的數據?
Deleted?
存放進行delete?和update操作前的數據
--創建觸發器?
Create?trigger?User_OnUpdate??
On?ST_User??
for?Update?
As?
declare?@msg?nvarchar(50)?
--@msg記錄修改情況?
select?@msg?=?N'姓名從“'?+?Deleted.Name?+?N'”修改為“'?+?Inserted.Name?+?'”'?from?Inserted,Deleted?
--插入日志表?
insert?into?[LOG](MSG)values(@msg)?
--刪除觸發器?
drop?trigger?User_OnUpdate
七、存儲過程
--創建帶output參數的存儲過程?
CREATE?PROCEDURE?PR_Sum?
@a?int,?
@b?int,?
@sum?int?output
AS
BEGIN
set?@sum=@a+@b?
END
--創建Return返回值存儲過程?
CREATE?PROCEDURE?PR_Sum2?
@a?int,?
@b?int
AS
BEGIN
Return?@a+@b?
END
--執行存儲過程獲取output型返回值?
declare?@mysum?int
execute?PR_Sum?1,2,@mysum?output
print?@mysum?
--執行存儲過程獲取Return型返回值?
declare?@mysum2?int
execute?@mysum2=?PR_Sum2?1,2?
print?@mysum2
八、自定義函數
函數的分類:
1)標量值函數
2)表值函數
a:內聯表值函數
b:多語句表值函數
3)系統函數
--新建標量值函數?
create?function?FUNC_Sum1?
(?
@a?int,?
@b?int
)?
returns?int
as
begin
return?@a+@b?
end
--新建內聯表值函數?
create?function?FUNC_UserTab_1?
(?
@myId?int
)?
returns?table
as
return?(select?*?from?ST_User?where?ID@myId)?
--新建多語句表值函數?
create?function?FUNC_UserTab_2?
(?
@myId?int
)?
returns?@t?table
(?
[ID]?[int]?NOT?NULL,?
[Oid]?[int]?NOT?NULL,?
[Login]?[nvarchar](50)?NOT?NULL,?
[Rtx]?[nvarchar](4)?NOT?NULL,?
[Name]?[nvarchar](5)?NOT?NULL,?
[Password]?[nvarchar](max)?NULL,?
[State]?[nvarchar](8)?NOT?NULL
)?
as
begin
insert?into?@t?select?*?from?ST_User?where?ID@myId?
return
end
--調用表值函數?
select?*?from?dbo.FUNC_UserTab_1(15)?
--調用標量值函數?
declare?@s?int
set?@s=dbo.FUNC_Sum1(100,50)?
print?@s?
--刪除標量值函數?
drop?function?FUNC_Sum1
談談自定義函數與存儲過程的區別:
一、自定義函數:
1.?可以返回表變量
2.?限制頗多,包括
不能使用output參數;
不能用臨時表;
函數內部的操作不能影響到外部環境;
不能通過select返回結果集;
不能update,delete,數據庫表;
3.?必須return?一個標量值或表變量
自定義函數一般用在復用度高,功能簡單單一,爭對性強的地方。
二、存儲過程
1.?不能返回表變量
2.?限制少,可以執行對數據庫表的操作,可以返回數據集
3.?可以return一個標量值,也可以省略return
存儲過程一般用在實現復雜的功能,數據操縱方面。
1、升級硬件,使用高性能的存儲設備
2、這數據量級,SQL的數據庫使用分區表是個非常好的選擇。若是分區表+多臺存儲服務設備,效果肯定杠杠的
3、主要矛盾是集中在IO吞吐上,所以解決了IO吞吐速度,就相當于解決了一半問題
4、在設計表的時候,每一列都要謹慎設置列長度和列類型,既要滿足存儲內容的需要,又要盡可能的短一些。
只能幫到這個地步了
create
procedure
prCreateSubPlan
as
begin
declare
@id
int,
@intCycle
int,
@planName
varchar(100),
@createTime
smalldatetime,
@cycleTime
int
select
@id
=
min(t_cplan_id)
from
t_cplan
while
(@id
is
not
null)
begin
select
@planName=t_plan_name,
@createTime
=
createTime,
@cycleTime
=
cycleTime
from
t_cplan
where
t_cplan_id=@id
select
@intCycle=
while
(@intCycle@cycleTime)
begin
--
表t_plan
列t_plan_id是IDENTITY
列
insert
t_plan
(t_plan_name,
t_cplan_id,
createTime)
values
(@planName,
@id,
dateadd(day,
@intCycle,
@createTime))
select
@intCycle
=
@intCycle
+
1
end
select
@id
=
min(t_cplan_id)
from
t_cplan
where
t_cplan_id@id
end
end
go