存儲過程與SQL語句是怎樣的
我的一位朋友說:他從臺灣知名技術作家李維先生的一本書中獲悉,如果用存儲過程封裝SQL語句,系統效率將有極大提升。
他做過實驗!!! --我相信朋友做過實驗,盡管非親眼所見。不過我估計他的實驗有問題,那樣的實驗不但蒙蔽了他,也蒙蔽了李維先生(如果他的著作中的內容沒有被誤會),甚至更多的人。
然而我必須拿出證據,方能使人信服。 后來遇到一個具體的問題:客戶端經常要向數據庫插入記錄。
在J2EE中,一個 Entity Bean Home 的 create 方法調用中,一般就沒用存儲過程。朋友立馬在觀點上持反對意見( 可能是因為他暫時有來得及否決J2EE ),認為要是J2EE能夠將“插入記錄”諸如此類動作改為對存儲過程的調用就好了。
我們因此再次發生爭論(我僅是反對朋友的看法,但也沒提出任何我自己的看法,因為要下一個結論是很不容易的)。最后我不得已而做了實驗,分別在 Oracle 10g 和 postgreSQL 8。
0。1 上。
實驗內容如下: A、建表腳本: create table ztest( fieldA integer primary key, fieldB varchar(128), fieldC varchar(128) ) B、客戶端請求 DBMS 執行的 insert SQL語句: insert into ztest values( ?1, ?2, ?3 ); -- ?1,?2,?3 將在運行時以合理的值替代之 C、客戶端調用的存儲過程(JDBC CallableStatement 調用): Oracle:(調用方式 call up_add(。 。
。),) create or replace procedure up_add( fieldA integer, fieldB varchar, fieldC varchar ) is begin insert into ztest values( fieldA, fieldB, fieldC); end; postgreSQL:(客戶端調用方式 select uf_add(。
。
) ) CREATE OR REPLACE FUNCTION uf_add (integer, varchar, varchar) RETURNS void AS' begin insert into ztest values($1,$2,$3); return; end; 'LANGUAGE 'plpgsql' VOLATILE RETURNS NULL ON NULL INPUT SECURITY INVOKER; D、環境: postgreSQL:數據庫服務器與客戶端程序“都在本機”并“同時運行” Oracle: 獨立數據庫服務器(測試時始終有人在慢慢打字,應該對機器性能無影響) 測試: 通過不同方式( 即 請求DBMS執行SQL語句 和 調用DBMS邏輯等價的存儲過程)向測試表中連續加入 1024 記錄 經多次反復測試,得結果如下 postgreSQL: 兩種方式下,測試時間均為 21- 24 seconds 之間 (每個結果的測試環境一致) Oracle: 8次 SQL 執行請求分別用時(ms) 5422 4750 3875 3812 5672 3531 3484 3547 6次 存儲過程調用分別用時(ms) 4578 4500 6297 4219 4547 5734 (每個結果的測試環境一致)由此可知,存儲過程封裝簡單的 SQL 語句,效率相當,且可能更低。 但很多朋友的確得出結論:存儲過程的確比SQL快。
為什么?---- 因為他們測試時寫了一個不具實際意義,同時也與SQL語句的“一次客戶端調用”不具可比性的測試用存儲過程。Oracle PL/SQL 描述方式如下,該方法一次調用就可以向數據表添加 1024 條記錄,連網絡通訊都省了。
怪不得性能有“千倍差異”! create or replace procedure up_add( ) is declare n:integer; begin n := 0; while( n 它與客戶端一次提交單條 SQL 語句沒有可比性:當一次只需要向DBMS提交一條新記錄,要這個存儲過程干什么呢?。
sql數據庫備份語句
--完整備份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_*'
--差異備份
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Diff_*'
With Differential
--日志備份,默認截斷日志
Backup Log NorthwindCS
To disk='G:\Backup\NorthwindCS_Log_*'
--日志備份,不截斷日志
Backup Log NorthwindCS
To disk='G:\Backup\NorthwindCS_Log_*'
With No_Truncate
--截斷日志不保留
Backup Log NorthwindCS
With No_Log
--或者
Backup Log NorthwindCS
With Truncate_Only
--截斷之后日志文件不會變小
--有必要可以進行收縮
--文件備份
Exec Sp_Helpdb NorthwindCS --查看數據文件
Backup Database NorthwindCS
File='NorthwindCS' --數據文件的邏輯名
To disk='G:\Backup\NorthwindCS_File_*'
--文件組備份
Exec Sp_Helpdb NorthwindCS --查看數據文件
Backup Database NorthwindCS
FileGroup='Primary' --數據文件的邏輯名
To disk='G:\Backup\NorthwindCS_FileGroup_*'
With init
--分割備份到多個目標
--恢復的時候不允許丟失任何一個目標
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Full_*'
,disk='G:\Backup\NorthwindCS_Full_*'
--鏡像備份
--每個目標都是相同的
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Mirror_*'
Mirror
To disk='G:\Backup\NorthwindCS_Mirror_*'
With Format --第一次做鏡像備份的時候格式化目標
--鏡像備份到本地和遠程
Backup Database NorthwindCS
To disk='G:\Backup\NorthwindCS_Mirror_*'
Mirror
To disk='\\192.168.1.200\Backup\NorthwindCS_Mirror_*'
With Format
--每天生成一個備份文件
Declare @Path Nvarchar(2000)
Set @Path ='G:\Backup\NorthwindCS
sql自己寫的語句可以保存嗎
(1)直接CTRL+S保存在電腦上就行了,生成一個.SQL的文件他就相當于一個文本,保存你的sql語句。
(2)你說的可能是切換了數據庫,你注意這個可以切換數據庫,然后你的sql語句是在這個當前數據庫里執行的
sql 存儲過程查詢語句
在數據庫中先創建存儲過程!具體代碼如下:
create proc sp_GetTesttableByname --sp_GetTesttableByname代表存儲過程名稱
as
begin
SELECT nickname,email FROM testtable WHERE name='張三' --存數過程的查詢語句
end
go
exec sp_GetTesttableByname; --查詢存儲過程結果
按下F5執行以上代碼之后 然后再在項目中寫調用存儲過程語句!希望我的回答對你有所幫助,謝謝采納!
用sql寫一個簡單的存儲過程語句
CREATE procedure Sp_select_Time
@Teacher_ID char(12),
@ReplyTime char(20),
@returnTimeVal int output
as
begin
if exists(select *
from
TeacherReply
where Teacher_ID=@Teacher_ID and ReplyTime=@ReplyTime)
set @returnTimeVal=0
else
set @returnTimeVal=1
end
return
GO
sql存儲過程語句求詳解,下面的是內容
很簡單的,樓主回答的很正確啊,就是創建個臨時表,把從多個表中查出的數據放入臨時表,你可以直接把臨時表理解成自定義要顯示的格式,后面的update就是更新臨時表中的數據。
我寫這個寫的很多給你個例子,比較清晰的:-- ================================================-- Template generated from Template Explorer using:-- Create Procedure (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 procedure.-- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO-- =============================================-- Author: -- Create date: -- Description: -- =============================================----@year int = year(getdate()),--@month int = month(getdate()) CREATE PROCEDURE EmloyeeAlterationReport @year int ,@month int AS BEGIN select department,0 ondutybegmon,0 ondutyendmon,0 moninduty,0 monoutduty,0 monmove,0 monintoonduty,employeeid monoutrate ,id into #1 from Qn_EmloyeeInfo where 1 = 2 insert into #1(department,ondutybegmon,ondutyendmon,moninduty,monoutduty,monmove,monintoonduty,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) '' group by department --月末在職人數 select department, count(*) reccount into #3 from Qn_EmloyeeInfo where year(createddate) '' group by department --本月入職人數 select department, count(*) reccount into #4 from HR_ContractInfo_formal where year(staffDate) '' group by department --本月離職人數 select department, count(*) reccount into #5 from HR_ContractInfo_formal where year(leaveDate) '' and status in ('離職') group by department --本月異動人數 select newdepartment department, count(*) reccount into #6 from HR_StaandPayChangeList where changeType In ('部門變動','崗位變動') and year(changeDate) '' group by newdepartment --本月轉正人數 select department, count(*) reccount into #7 from HR_ContractInfo where year(formalDate) '' and status in ('轉正') and isnull(department,'') '' group by department --更新月初在職人數 update #1 set ondutybegmon = #*nt from #2 where #*ment = #*ment --更新月末在職人數 update #1 set ondutyendmon = #*nt from #3 where #*ment = #*ment --更新本月入職人數 update #1 set moninduty = #*nt from #4 where #*ment = #*ment --更新本月離職人數 update #1 set monoutduty = #*nt from #5 where #*ment = #*ment --更新本月異動人數 update #1 set monmove = #*nt from #6 where #*ment = #*ment --更新本月轉正人數 update #1 set monintoonduty = #*nt from #7 where #*ment = #*ment --查詢報表 select * from #1 END GO。