當前位置:首頁 » 編程語言 » sql存儲的例子
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql存儲的例子

發布時間: 2022-08-20 14:14:21

1. sql 存儲過程 實例 貼源碼就好了

例子:
CREATE
PROCEDURE
order_tot_amt
@o_id
int,
@p_tot
int
output
AS
SELECT
@p_tot
=
sum(Unitprice*Quantity)
FROM
orderdetails
WHERE
ordered=@o_id
GO
例子說明:
該例子是建立一個簡單的存儲過程order_tot_amt,這個存儲過程根據用戶輸入的定單ID號碼(@o_id),由定單明細表
(orderdetails)中計算該定單銷售總額[單價(Unitprice)*數量(Quantity)],這一金額通過@p_tot這一參數輸出給調用這一存儲過程的程序。

2. 帶輸出參數的存儲過程PL/SQL的小例子

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace StoreProceTest
{
public class Program
{
/**
* 存儲過程
*
* create procere queryStuNameById
@stuId varchar(10),--輸入參數
@stuName varchar(10) output --輸出參數
as
select @stuName=stuName from stuInfo where stuId=@stuId
*
*/

static void Main(string[] args)
{
Operater op = new Operater();
string name = op.QueryStuNameById("1234");
Console.WriteLine(string.Format("學號為1234的學生的姓名為{0}", name));
}
}
public class Operater
{
private string ConStr = "server=.;database=User;uid=sa;pwd=1234";
private SqlConnection sqlCon = null;
private SqlCommand sqlComm = null;
SqlDataReader dr = null;
public string QueryStuNameById(string Id)
{
string name = "";
try
{
using (sqlCon = new SqlConnection(ConStr))
{
sqlCon.Open();
sqlComm = new SqlCommand("queryStuNameById", sqlCon);
//設置命令的類型為存儲過程
sqlComm.CommandType = CommandType.StoredProcere;
//設置參數
sqlComm.Parameters.Add("@stuId", SqlDbType.VarChar);
//注意輸出參數要設置大小,否則size默認為0,
sqlComm.Parameters.Add("@stuName", SqlDbType.VarChar, 10);
//設置參數的類型為輸出參數,默認情況下是輸入,
sqlComm.Parameters["@stuName"].Direction = ParameterDirection.Output;
//為參數賦值
sqlComm.Parameters["@stuId"].Value = "1234";
//執行
sqlComm.ExecuteNonQuery();
//得到輸出參數的值,把賦值給name,注意,這里得到的是object類型的,要進行相應的類型輪換
name = sqlComm.Parameters["@stuName"].Value.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return name;
}
}
}

3. mssql存儲過程

MS
SQL基礎教程:創建存儲過程
在MS
SQL
Server
2000
中,創建一個存儲過程有兩種方法:一種是使用Transaction-SQL
命令Create
Procere,
另一種是使用圖形化管理工具Enterprise
Manager。
用Transaction-
SQL
創建存儲過程是一種較為快速的方法,但對於初學者,使用Enterprise
Manager
更易理解,更為簡單。
當創建存儲過程時,需要確定存儲過程的三個組成部分;
所有的輸入參數以及傳給調用者的輸出參數。
被執行的針對資料庫的操作語句,包括調用其它存儲過程的語句;
返回給調用者的狀態值,以指明調用是成功還是失敗。
12.2.1
使用Enterprise
Manager
創建存儲過程
按照下述步驟用Enterprise
Manager
創建一個存儲過程:
啟動Enterprise
Manager,
登錄到要使用的伺服器。
選擇要創建存儲過程的資料庫,在左窗格中單擊Stored
Procere
文件夾,此時在右窗格中顯示該資料庫的所有存儲過程,如圖12-1
所示。
右擊Stored
Procere
文件夾,在彈出菜單中選擇New
Stored
Procere,
此時打開創建存儲過程對話框,
輸入存儲過程正文。
單擊Check
Syntax,
檢查語法是否正確。
單擊OK,
保存。
在右窗格中,右擊該存儲過程,在彈出菜單中選擇All
task,
選擇
ManagePermissions,
設置許可權,
12.2.2
用CREATE
PROCEDURE
命令創建存儲過程
通過運用Create
Procere
命令能夠創建存儲過程,在創建存儲過程之前,應該考慮到以下幾個方面:
在一個批處理中,Create
Procere
語句不能與其它SQL
語句合並在一起;
資料庫所有者具有默認的創建存儲過程的許可權,它可把該許可權傳遞給其它的用戶;
存儲過程作為資料庫對象其命名必須符合命名規則;
只能在當前資料庫中創建屬於當前資料庫的存儲過程。
用Create
Procere
創建存儲過程的語法規則如下:
CREATE
PROC
[
EDURE
]
procere_name
[
;
number
]
[
{
@parameter
data_type
}
[
VARYING
]
[
=
default
]
[
OUTPUT
]
]
[
,...n
]
[
WITH
{
RECOMPILE
|
ENCRYPTION
|
RECOMPILE
,
ENCRYPTION
}
]
[
FOR
REPLICATION
]
AS
sql_statement
[
...n
]

4. sql 存儲過程是怎麼實現的 簡單的例子和解釋!

存儲過程就是一組保存在資料庫中的sql語句,在需要的時候可以調用

最簡單的,比如

create procere test as
delete from t_1; ---刪除t_1表的所有記錄
在sql server查詢分析器執行時:

exec test; --執行過程test,刪除了表t_1的所有記錄

當然,沒有人這樣使用存儲過程,存儲過程可以接受參數,處理大量sql語句,並返回結果。
當在編寫軟體的過程中,碰到需要進行復雜的資料庫操作時,可能需要大量的sql語句,這時候可以先在資料庫中創建存儲過程,將sql語句都寫在存儲過程里,可以視情況加入參數,也可以返回處理結果。編寫軟體時,在適當的地方引用並執行這個存儲過程就好了,至於怎麼引用,不同的軟體開發語言有不同的語法。
存儲過程是預編譯的,這樣可以提高執行效率,對於軟體代碼的維護也有好處

5. SQL語句問題:存儲過程定義是什麼什麼時候用它作用是什麼怎樣寫,來個實例!

定義:存儲過程是一組為了完成特定功能的SQL語句集,是利用SQL Server所提供的Transact-SQL語言所編寫的程序。作用:將常用或復雜的工作,預先用SQL語句寫好並用一個指定名稱存儲起來, 以後需要資料庫提供與已定義好的存儲過程的功能相同的服務時,只需調用execute,即可自動完成命令。什麼時候用:提高資料庫執行速度,對資料庫進行復雜操作,重復使用,安全要求高例子: CREATE PROCEDURE order_tot_amt @o_id int, @p_tot int output AS SELECT @p_tot = sum(Unitprice*Quantity) FROM orderdetails WHERE ordered=@o_id GO

6. 用sql寫一個簡單的存儲過程語句

insert into 表名 (欄位1,欄位2,....)values('"欄位1的值"','"&欄位2的&"',...)
上述為ASP中增加數據的SQL方法

7. sql 存儲過程語句編寫

要到達你的要求,在存儲過程中必須使用動態SQL語句。


一個簡化的例子:

createprocereMyDynamicSQL
@tblwherenvarchar(200)--a==aora==xxx
as
begin
declare@sqlnvarchar(max)
--動態拼接sql語句
set@sql=N'select*from[表一]where'+@tblwhere
--執行
executesp_executesql@sql
end

8. sql存儲過程語句求詳解,下面的是內容

很簡單的,樓主回答的很正確啊,就是創建個臨時表,把從多個表中查出的數據放入臨時表,你可以直接把臨時表理解成自定義要顯示的格式,後面的update就是更新臨時表中的數據。
我寫這個寫的很多給你個例子,比較清晰的:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procere (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procere.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--
--@year int = year(getdate()),
--@month int = month(getdate())

CREATE PROCEDURE EmloyeeAlterationReport
@year int ,
@month int
AS
BEGIN
select department,0 ontybegmon,0 ontyendmon,0 moninty,0 monoutty,0 monmove,0 monintoonty,employeeid monoutrate ,id into #1 from Qn_EmloyeeInfo where 1 = 2

insert into #1(department,ontybegmon,ontyendmon,moninty,monoutty,monmove,monintoonty,monoutrate)
select department,0,0,0,0,0,0,0
from Qn_EmloyeeInfo
where year(createddate) = convert(int , '2011') and month(createddate) = 4 and isnull(department,'') <> ''
group by department

--月初在職人數
select department, count(*) reccount into #2 from Qn_EmloyeeInfo where year(createddate) <= convert(int , '2011') and month(createddate) < (6-1) and isnull(department,'') <> '' group by department
--月末在職人數
select department, count(*) reccount into #3 from Qn_EmloyeeInfo where year(createddate) <= convert(int , '2011') and month(createddate) <= (6-1) and isnull(department,'') <> '' group by department
--本月入職人數
select department, count(*) reccount into #4 from HR_ContractInfo_formal where year(staffDate) <= convert(int , '2011') and month(staffDate) = (6-1) and isnull(department,'') <> '' group by department
--本月離職人數
select department, count(*) reccount into #5 from HR_ContractInfo_formal where year(leaveDate) <= convert(int , '2011') and month(leaveDate) = (6-1) and isnull(department,'') <> '' and status in ('離職') group by department
--本月異動人數
select newdepartment department, count(*) reccount into #6 from HR_StaandPayChangeList where changeType In ('部門變動','崗位變動') and year(changeDate) <= convert(int , '2011') and month(changeDate) = (6-1) and isnull(newdepartment,'') <> '' group by newdepartment
--本月轉正人數
select department, count(*) reccount into #7 from HR_ContractInfo where year(formalDate) <= convert(int , '2011') and month(formalDate) = (6-1) and isnull(department,'') <> '' and status in ('轉正') and isnull(department,'') <> '' group by department

--更新月初在職人數
update #1
set ontybegmon = #2.reccount
from #2
where #1.department = #2.department
--更新月末在職人數
update #1
set ontyendmon = #3.reccount
from #3
where #1.department = #3.department

--更新本月入職人數
update #1
set moninty = #4.reccount
from #4
where #1.department = #4.department

--更新本月離職人數
update #1
set monoutty = #5.reccount
from #5
where #1.department = #5.department

--更新本月異動人數
update #1
set monmove = #6.reccount
from #6
where #1.department = #6.department

--更新本月轉正人數
update #1
set monintoonty = #7.reccount
from #7
where #1.department = #7.department

--查詢報表
select * from #1

END
GO

9. SQL存儲過程實例

樓下的太麻煩了吧。emp員工表,輸入任何部門號,返回部門的總工資,把總工資和部門好,分別放進emp2表裡。
編寫存儲過程查詢某部門員工的工資總和
create or replace procere my_text(v_deptno number)
is
cursor c is select* from emp;
v1 number:=0;
begin
for a in c loop
if(a.deptno=v_deptno) then
v1:=v1+a.sal;
end if;
end loop;
insert into emp2 values(v1,v_deptno);
end;
是不是很吊啊?樓主?

10. sql存儲過程實例

CREATE OR REPLACE
procere procere_name
begin
for c in (select column_a_name from table_a_name)loop
update table_b_name set column_b_name=c.column_a_name where....
end loop;
end;