Notes 3 stored procedure sql

16:22 2014/1/26
First, the definition of variables
- simple assignment
declare @a int

set @a = 5

print @a

--使用select语句赋值
declare @user1 nvarchar(50)
select @user1 = 'abc'
print @user1
declare @user2 nvarchar(50)
select @user2 = Name from ST_User where Id = 1
print @user2

- assignment update statements
DECLARE @ USER3 nvarchar (50)
update the Name ST_User SET WHERE ID = @ = USER3. 1
Print @ USER3

Second, the tables, temporary tables, table variables

- creating a temporary table. 1
Create Table # DU_User1
(
[ID] [int] the NOT NULL,
[the Oid] [int] the NOT NULL,
[the Login] [nvarchar] (50) the NOT NULL,
[Rtx] [nvarchar] (. 4) NULL the NOT,
[the Name] [nvarchar] (. 5) the NOT NULL,
[Password] [nvarchar] (max) NULL,
[State] [nvarchar] (. 8) the NOT NULL
);
-. 1 insert a record into the temporary table
insert into # DU_User1 (ID, Oid, [ Login], Rtx, Name, [Password], State) values (100,2, 'LS', '0000', ' temporary', '321', 'special');

- data from the query ST_User filled into newly generated temporary table
select * into # DU_User2 from ST_User where ID <8

- joint inquiry and two temporary tables
select * from # DU_User2 where ID < 3 union select * from # DU_User1

- Remove the two temporary tables
drop the Table # DU_User1
drop the 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,
)

- The query result set (a plurality of data) into the temporary table
INSERT INTO SELECT * from #t ST_User
- can not be so inserted
--select * into #t from dbo.ST_User

- Add one for self-growth sub-segments int
the ALTER the Table #t the Add [myid] int the NOT NULL IDENTITY (1, 1)
- Add a default fill a globally unique identifier
alter table #t add [myid1] uniqueidentifier NOT NULL default (newid ())

select * from #t
drop table #t

 

 

- query result set to increase from the growth column

--无主键时:
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


Third, circulation

--while loop calculation 1-100 and
DECLARE @a int
DECLARE @sum int
SET. 1 = A @
SET @ SUM = 0
the while @a <= 100
the begin
SET @sum @ + = A
SET @ + A. 1 =
End
Print @sum


Fourth, conditional statements

--if, else conditional branching
IF (. 1. 1 + = 2)
the begin
Print 'to'
End
the else
the begin
Print 'wrong'
End

--when then conditional branch
DECLARE @Today int
DECLARE @week nvarchar (. 3)
SET = @ Today. 3
SET = Week @ Case
When the then. 1 = @ Today 'Monday'
When @ 2 = the then Today 'Tuesday'
When Today @ = 3 then 'Wednesday'
the when the then 4 = @ Today 'Thursday'
the when the then 5 = @ Today 'Friday'
the when the then 6 = @ Today 'Saturday'
the when the then 7 = @ Today 'Sunday'
the else 'wrong value'
End
Print @week

 

 

Fifth, the cursor

declare @ID int
declare @Oid int
declare @Login varchar(50)

- define a cursor
DECLARE user_cur ID Cursor for SELECT, the Oid, [the Login] from ST_User
- open cursors
Open user_cur
the while @@ = 0 fetch_status
the begin
- read the cursor
fetch next from user_cur into @ ID, @ Oid, @ Login
@ID Print
--print @login
End
use Close user_cur
- to destroy the cursor
deallocate user_cur


Sixth, the flip-flop

  Triggers a temporary table:

  Inserted
  data storage insert and update operations performed
  Deleted
  storing data before update and delete operations

- Create trigger
the Create Trigger User_OnUpdate
the On ST_User
for the Update
of As
DECLARE @msg nvarchar (50)
- the revision of recording @ MSG
select @msg = N 'from the name "' + Deleted.Name + N '" modify "' + + Inserted.Name ' "' from the inserted, deleted
- inserting the log table
iNSERT INTO [the lOG] (the MSG) values (@msg)

- delete triggers
drop trigger User_OnUpdate

 

Seven, stored procedures

- Create a stored procedure with the output parameter of
the CREATE PROCEDURE PR_Sum
@a int,
@b int,
@sum int output
the AS
the BEGIN
SET @ @ SUM = A + B @
the END

- Create a stored procedure return value Return
the CREATE PROCEDURE PR_Sum2
@a int,
@b int
the AS
the BEGIN
Return @ @ A + B
the END

- executes a stored procedure return value acquired output type
DECLARE @mysum int
Execute PR_Sum 1,2, @ mySum output
Print @mysum

- executes a stored procedure acquires the return value Return Type
DECLARE @ mysum2 int
Execute @ mysum2 = 1,2 PR_Sum2
Print @ mysum2


 

Eight custom function

  Function Category:

    1) scalar-valued function

    2) the value of the function table

        a: inline table valued function

        b: a multi-statement table-valued function

    3) system function

  

- New scalar-valued function
Create function FUNC_Sum1
(
@a int,
@b int
)
Returns int
AS
the begin
return @ + A @ B
End

- New inline table valued function
Create function FUNC_UserTab_1
(
@myId int
)
Returns Table
AS
return (from ST_User SELECT * 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

- the value of the function call table
SELECT * from dbo.FUNC_UserTab_1 (15)
- scalar-valued function call
DECLARE @s int
SET @ dbo.FUNC_Sum1 S = (100,50)
Print @s

- Delete scalar-valued function
drop function FUNC_Sum1


Talk-defined functions and stored procedures difference:

A custom function:

  Table 1. The variable may return

  2. many restrictions, including

    Output parameter can not be used;

    You can not use a temporary table;

    Function can not affect operation of the internal to the external environment;

    Select can not return a result set;

    You can not update, delete, database tables;

  3. must return a scalar value or a table variable

  Custom functions are typically used in high multiplexing, single function simple, strong contention for a place.

Second, stored procedures

  Table 1. Variables not return

  2. less restrictions, can operate on the database table, a data set may return

  3. You can return a scalar value, can be omitted return

   Stored in the process it is generally used to implement complicated functions, data manipulation aspect.

  

  

Guess you like

Origin www.cnblogs.com/missheyo/p/11237766.html