SQL server中如何存儲:
首先准備數據,測試存儲過程
use ssqadm;
創建測試books表
create table books_test ( book_id int identity(1,1) primary key,
book_name varchar(20),book_price float,book_auth varchar(10));
插入測試數據
insert into books_test (book_name,book_price,book_auth)values
('論語',25.6,'孔子'),
('天龍八部',25.6,'金庸'),
('雪山飛狐',32.7,'金庸'),
('平凡的世界',35.8,'路遙'),
('史記',54.8,'司馬遷');
select * from books_test;*/
創建無參存儲過程
if (exists (select * from sys.objects where name = 'getAllBooks'))
drop proc getAllBooks
go
create procere getAllBooks
as
begin
select * from books_test;
調用,執行存儲過程
exec getAllBooks;
end
go
修改存儲過程
alter procere getallbooks
as
select book_name from books_test;
修改存儲過程的名稱
sp_rename getallbooks,proc_get_allbooks;
go
exec proc_get_allbooks;
go
創建帶參數的存儲過程
use ssqadm
go
if (exists (select * from sys.objects where name = 'searchbooks'))
drop proc searchbooks
exec searchbooks
執行存儲searchbooks得到如下結果:
go
create procere searchbooks (@bookid int)--括弧裡面是
as
begin
declare @book_id int;定義一個標量變數,只是保證存儲過程的完整性,在本存儲是多此一舉的。
set @book_id = @bookid;
select* from books_test where book_id = @book_id;
end;
go
-- exec searchbooks
執行存儲searchbooks得到如下結果:
創建帶兩個參數的存儲過程
use ssqadm
go
if (exists (select * from sys.objects where name = 'book_test2'))
drop proc book_test2
exec book_test2
執行存儲book_test2得到如下結果:
go
create procere book_test2
(@bookid int,@bookname varchar(20))括弧裡面是
as
begin
declare @book_id int;
定義一個標量變數,只是保證存儲過程的完整性,在本存儲是多此一舉的。
declare @book_name varchar(20);
set @book_id = @bookid;
set @book_name = @bookname;
select* from books_test where book_id =
@book_id and book_name = @book_name;
end;
go
exec book_test2
(1)sql登陸存儲過程擴展閱讀:
SQL Server中查詢存儲命令子句:
USE [SSQADM]
Use 是跳轉到哪個資料庫,對這個資料庫進行操作。
GO
GO向 SQL Server 實用工具發出一批 Transact-SQL 語句結束的信號,相當於提交上面的SQL語句。
GO是把t-sql語句分批次執行
(一步成功了才會執行下一步,即一步一個GO)
/****** Object: StoredProcere [dbo].[PROC_four_five_hr]
Script Date: 07/30/2018 13:44:55 ******/
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ON
2. SQL存儲過程如何調用存儲過程
1、首先先創建一個存儲過程,代碼如圖,存儲過程主要的功能是為表JingYan插入新的數據。
3. C#中調用SQL存儲過程實現登錄認證代碼
存儲過程如下set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER
procere [dbo] [security_check](@user_sort int @userID nchar( ) @userPWD nchar( ) )
as
declare @uid nchar( );
declare @pwd nchar( );
declare @state bit;
set @state= ;
if( @user_sort= )
begin
declare cursor_temp cursor local for 定義游標
select 學號 密碼 from 學生表 where 學號 = @userID and 密碼 = @userPWD;
open cursor_temp; 打開游標
fetch cursor_temp into @uid @pwd; 推進游標
close cursor_temp;虧簡 關閉游標
end
if( @uid =@userID and @pwd=@userPWD )
begin
set @state= ;
return @state;
end;
else
begin
set @state= ;
return @state;
end;
C#代碼如下
using System;using System Collections Generic;using System Text;using System Data;using System Data SqlClient;
namespace 密碼驗證{ class security { public static DataSet check(string uid) { SqlConnection mySqlConnection = new SqlConnection( server= \SqlExpress;database=XSXK;integrated security=SSPI );
mySqlConnection Open();
SqlCommand mySqlCommand = mySqlConnection CreateCommand();
mySqlCommand CommandText = select * from 學生表 where 學號 = + uid;
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
DataSet myDataSet = new DataSet() ;
&銷態褲nbsp;閉余 mySqlDataAdapter SelectCommand = mySqlCommand ;
mySqlDataAdapter Fill (myDataSet 學生表 );
mySqlConnection Close();
return myDataSet;
}
public static bool secuirty_check(int user_select string uid string pwd) { int result = ;
SqlConnection mySqlConnection = new SqlConnection( server= \SqlExpress;database=XSXK;integrated security=SSPI );
SqlCommand mySqlCommand = new SqlCommand( security_check mySqlConnection );
mySqlCommand CommandType = CommandType StoredProcere;
// //關鍵部分!!
SqlParameter param = new SqlParameter();
param Direction = System Data ParameterDirection ReturnValue;
mySqlCommand Parameters Add(param);
// mySqlCommand Parameters Add( @user_sort SqlDbType Int ) Value = user_select ;
mySqlCommand Parameters Add( @userID SqlDbType NChar ) Value = uid ;
mySqlCommand Parameters Add( @userPWD SqlDbType NChar ) Value = pwd ;
//mySqlCommand Parameters[ @state ] Direction = ParameterDirection ReturnValue ;
mySqlConnection Open();
mySqlCommand ExecuteScalar();
mySqlConnection Close();
result = (int)param Value; if ( result == ) return false; else return true; } } class Program { static void Main(string[] args) {/* string uid; Console WriteLine( 請輸入用戶名 ); uid=Console ReadLine(); DataSet myDataSet = security check(uid);
foreach (DataRow thisDataRow in myDataSet Tables[ 學生表 ] Rows) {
Console WriteLine( 學號: + thisDataRow[ 學號 ]); Console WriteLine( 姓名: + thisDataRow[ 姓名 ]); Console WriteLine( 密碼: + thisDataRow[ 密碼 ]); } */
string uid; string pwd; Console WriteLine( 請輸入用戶名 ); uid = Console ReadLine(); Console WriteLine( 請輸入密碼 ); pwd = Console ReadLine(); if (security secuirty_check( uid pwd)) Console WriteLine( 登錄成功 ); else Console WriteLine( 用戶名或密碼錯誤! ); Console Read(); } }
}
lishixin/Article/program/net/201311/12756
4. SQL 登錄存儲過程問題
問題大了,如果你想取出用戶ID號,過程裡面根本沒有給@MemberID 賦值,你嫌羨所說的有返回不返回,只是裡面SELECT 語句芹模拍的作用,根本沒有意義,若要輸出ID需改寫成:
Create proc my_proc_UserLogin @UserName varchar(50),@UserPass varchar(50),@MemberID int output
as
begin
declare @ErrorNum int
set @ErrorNum=0
if exists(select * from tb_Member where Name=@UserName and Password=@UserPass)
begin
select @MemberID = MemberID from tb_Member where Name=@UserName and Password=@UserPass
end
else
begin
select @MemberID = 0
end
Return 0
end
這碼正樣執行
declare @id int
exec my_proc_UserLogin '1','1',@id output
select @id
才是得到的輸出ID值
5. sql server 2000 登錄的存儲過程
create proc lg_info
@loginID bit,
@loginPWD bit
as
if loginID =LoginId
begin
IsOnline=True
end
else
begin
print '賬號錯滲讓誤'
end
if IsOnline= 'True'
begin
loginPWD =@LoginPwd
select Id,LoginId,LoginPwd,IsOnline from Admin
end
else
begin
print '錯誤,無法登陸'
end
go
表裡若是只有一條信息這樣就行了!
如果還有多條信息!就不行了!
如果還不行就在提示孝氏下原因!
還有就是樓上的內中方法一起驗證密碼賬號!肯叢慎局定會出錯的!
6. Linux isql登陸後怎麼執行存儲過程
作業里定時執行存儲過程定時作業的制定
企業管理器
--管理
--SQL Server代理
--右鍵作業
--新建作業
--"常規"項中輸入作業名稱
--"步驟"項
--新建
--"步驟名"中團塵輸入步驟名
--"類型"中選擇"Transact-SQL 腳本(TSQL)"
--"資料庫"選擇執行命令的資料庫
--"命令"中輸入要執行的語句賀或改:
EXEC 存儲過程名 ... --該存儲過程用於創建表
--確定
--"調度"項
--新建調度
--"名稱"中輸入調度名稱
--"調度類型"中選擇你的作業執行安排
--如果選擇"反復出現"
--點"更改"來設置你的時間安排
然後將SQL Agent服務啟動,並設置為自動啟動,否則你的作業不會被執行
設置禪判方法:
我的電腦--控制面板--管理工具--服務--右鍵 SQLSERVERAGENT--屬性--啟動類型--選擇"自動啟動"--確定.
7. SQL SERVER 如何應用存儲過程呢操作方法有什麼呢
SQL SERVER 如何應用存儲過程呢?
首先最好在SQL SERVER的管理工具中通過create procere寫一條語句來創建存儲過程,創建語句後,點擊工具欄中的執行命令,消息欄中顯示命令已成功完成的消息,證明存儲過程已創建。然後就可以在存儲過程子文件夾下看到自己創建的存儲過程了,執行存儲過程,可以使用exec命令,後跟存儲過程的名稱,另外,還可以在創建存儲過程的時候傳入參數,如下圖,需要使用@符號傳入參數,如果你的存儲過程加了參數,那麼如果你調用的時候沒有傳入參數,SQL SERVER會提示錯誤。
3、存儲過程減少網路流量對於資料庫對象的相同操作,如果將此次操作所涉及的T-SQL語句組織成一個存儲過程,在客戶端調用該存儲過程時,只在網路上傳遞調用語句,否則會是多條 SQL 語句。從而減輕了網路流量,降低了網路負載存儲過程可以用作安全機制,系統管理員可以對要執行的存儲過程的許可權進行限制,從而限制對某些數據的訪問,避免未經授權的用戶訪問數據,保證數據安全。
8. sql2005 存儲過程在哪裡寫
1.進入 SQL Server Management Studio
2.登陸到你需要修改存儲過程的資料庫
3.按順序點擊你需要修改資料庫,可編程性,存儲過程
你可以在這里修改或新增存儲過程
ps:另外你也可以直接使用語句在SQL Server Management Studio的sql編輯界面中使用語句建立新的存儲過程
希望可以幫到你!