SQL SERVERプログラミングの基本文法
17608 ワード
一、変数を定義する
Insertedはinsertとudate操作後のデータDeletedを格納してdeleteとudate操作前のデータを保存します.
--
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_User1 where ID<8
--
select * from #DU_User2 where ID<3 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
五、ランドマークeclare @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とudate操作後のデータDeletedを格納してdeleteとudate操作前のデータを保存します.
--
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
八、関数--
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 IDcreate 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 IDend
--
select * from dbo.FUNC_UserTab_1(15)
--
declare @s int
set @s=dbo.FUNC_Sum1(100,50)
print @s
--
drop function FUNC_Sum1