oracle的建表sql转成sqlserver的建表sql时的注意点 :
1.所有的comment语句需要删除。
2.clob类型转换为text类型。
3.blob类型转换为image类型。
4.number类型转换为int,number(16,2)等转换为decimal(16,2),number(18)转换为bigint。
5.default sysdate改为default getdate()。
6.to_date('2009-12-18','yyyy-mm-dd')改为cast('2009-12-18' as datetime)
sqlserver:
变量的声明:
声明变量时必须在变量前加@符号
declare @i int
变量的赋值:
变量赋值时变量前必须加set
set @i = 30
声明多个变量:
declare @s varchar(10),@a int
if语句:
java代码
if ..
begin
...
end
else if ..
begin
...
end
else
begin
...
end
example:
sql代码
declare @d int
set @d = 1
if @d = 1 begin
print '正确'
end
else begin
print '错误'
end
多条件选择语句:
example:
sql代码
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
循环语句:
java代码
while 条件 begin
执行语句
end
example:
java代码
declare @i int
set @i = 1
while @i<1000000 begin
set @i=@i+1
end
定义游标:
sql代码
declare @cur1 cursor for select .........
open @cur1
fetch next from @cur1 into 变量
while(@@fetch_status=0)
begin
处理.....
fetch next from @cur1 into 变量
end
close @cur1
deallocate @cur1
sql代码
as
declare @category_ci_tablename varchar(50) =''
declare @result varchar(2000) = ''
declare @ci_id decimal = 0
declare @num int = 1
declare @countnum int = 1
begin
select @countnum = count(attribute_config_id) from t_attribute_config where cmdb_update_flag= 'y' and category_code =@category_code
if (@attribute2='a')
begin
declare mycursor cursor for select attribute_config_code from t_attribute_config where cmdb_update_flag= 'y' and category_code =@category_code
open mycursor fetch next from mycursor into @config_code
set @result = @result+@config_code+','
while @@fetch_status = 0
begin
fetch next from mycursor into @config_code
set @num = @num+ 1
if(@num<@countnum)
begin
set @result = @result+@config_code+','
end
else if(@num=@countnum)
begin
set @result = @result +@config_code
end
end
close mycursor
deallocate mycursor
set @result = 'insert into ' + @attribute1 + '(' + @result +') select '+ @result +' from '+@category_ci_tablename +' where ci_order_line_id='+@key_id
end
else if((@attribute2='u'))
临时表:
-- select into 从一个查询的计算结果中创建一个新表。 数据并不返回给客户端,这一点和普通的select 不同。 新表的字段具有和 select 的输出字段相关联(相同)的名字和数据类型。
select * into newtable
from uname
-- insert into abc select
-- 表abc必须存在
-- 把表uname里面的字段username复制到表abc
insert into abc select username from uname
-- 创建临时表
create table #temp(
uid int identity(1, 1) primary key,
username varchar(16),
pwd varchar(50),
age smallint,
sex varchar(6)
)
-- 打开临时表
select * from #temp
1、局部临时表(#开头)只对当前连接有效,当前连接断开时自动删除。
2、全局临时表(##开头)对其它连接也有效,在当前连接和其他访问过它的连接都断开时自动删除。
3、不管局部临时表还是全局临时表,只要连接有访问权限,都可以用drop table #tmp(或者drop table ##tmp)来显式删除临时表。
临时表对执行效率应该影响不大,只要不是太过份,相反可以提高效率特别是连接查询的地方,只要你的数据库临时表空间足够
游标多,会严重执行效率,能免则免!
其他:
--有输入参数的存储过程--
create proc getcomment
(@commentid int)
as
select * from comment where commentid=@commentid
--有输入与输出参数的存储过程--
create proc getcommentcount
@newsid int,
@count int output
as
select @count=count(*) from comment where newsid=@newsid
--返回单个值的函数--
create function myfunction
(@newsid int)
returns int
as
begin
declare @count int
select @count=count(*) from comment where newsid=@newsid
return @count
end
--调用方法--
declare @count int
exec @count=myfunction 2
print @count
--返回值为表的函数--
create function getfunctiontable
(@newsid int)
returns table
as
return
(select * from comment where newsid=@newsid)
--返回值为表的函数的调用--
select * from getfunctiontable(2)
-----------------------------------------------------------------------------------------------------------------------------------
sqlserver 存储过程中不拼接sql字符串实现多条件查询
以前拼接的写法
set @sql=' select * from table where 1=1 '
if (@adddate is not null)
set @sql = @sql+' and adddate = '+ @adddate + ' '
if (@name a8093152e673feb7aba1828c43532094'' and is not null)
set @sql = @sql+ ' and name = ' + @name + ' '
exec(@sql)
下面是 不采用拼接sql字符串实现多条件查询的解决方案
第一种写法是 感觉代码有些冗余
if (@adddate is not null) and (@name a8093152e673feb7aba1828c43532094 '')
select * from table where adddate = @adddate and name = @name
else if (@adddate is not null) and (@name ='')
select * from table where adddate = @adddate
else if(@adddate is null) and (@name a8093152e673feb7aba1828c43532094 '')
select * from table where and name = @name
else if(@adddate is null) and (@name = '')
select * from table
第二种写法是
select * from table where (adddate = @adddate or @adddate is null) and (name = @name or @name = '')
第三种写法是
select * from table where
adddate = case @adddate is null then adddate else @adddate end,
name = case @name when '' then name else @name end
-----------------------------------------------------------------------------------------------------------------------------------
sqlserver存储过程基本语法
一、定义变量
--简单赋值
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_user 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 bymyid
--定义表变量
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
五、游标
declare @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和update 操作后的数据
deleted
存放进行delete 和update操作前的数据
--创建触发器
create trigger user_onupdate
on st_user
for update
as
declare @msg nvarchar(50)
--@msg记录修改情况
select @msg = n '姓名从“' + deleted. name + n '”修改为“' + inserted. name + '”' frominserted,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
八、自定义函数
函数的分类:
1)标量值函数
2)表值函数
a:内联表值函数
b:多语句表值函数
3)系统函数
--新建标量值函数
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 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
--调用表值函数
select * from dbo.func_usertab_1(15)
--调用标量值函数
declare @s int
set @s=dbo.func_sum1(100,50)
print @s
--删除标量值函数
drop function func_sum1
谈谈自定义函数与存储过程的区别:
一、自定义函数:
1. 可以返回表变量
2. 限制颇多,包括
不能使用output参数;
不能用临时表;
函数内部的操作不能影响到外部环境;
不能通过select返回结果集;
不能update,delete,数据库表;
3. 必须return 一个标量值或表变量
自定义函数一般用在复用度高,功能简单单一,争对性强的地方。
二、存储过程
1. 不能返回表变量
2. 限制少,可以执行对数据库表的操作,可以返回数据集
3. 可以return一个标量值,也可以省略return
存储过程一般用在实现复杂的功能,数据操纵方面。
-----------------------------------------------------------------------------------------------------------------------------------
sqlserver存储过程--实例
实例1:只返回单一记录集的存储过程。
表银行存款表(bankmoney)的内容如下
要求1:查询表bankmoney的内容的存储过程
create procedure sp_query_bankmoney
as
select * from bankmoney
go
exec sp_query_bankmoney
注* 在使用过程中只需要把t-sql中的sql语句替换为存储过程名,就可以了很方便吧!
实例2(向存储过程中传递参数):
加入一笔记录到表bankmoney,并查询此表中userid= zhangsan的所有存款的总金额。
create proc insert_bank @param1 char(10),@param2 varchar(20),@param3 varchar(20),@param4 int,@param5 int output
with encryption ---------加密
as
insert into bankmoney (id,userid,sex,money)
values(@param1,@param2,@param3, @param4)
select @param5=sum(money) from bankmoney where userid='zhangsan'
go
在sql server查询分析器中执行该存储过程的方法是:
declare @total_price int
exec insert_bank '004','zhangsan','男',100,@total_price output
print '总余额为'+convert(varchar,@total_price)
go
在这里再啰嗦一下存储过程的3种传回值(方便正在看这个例子的朋友不用再去查看语法内容):
1.以return传回整数
2.以output格式传回参数
3.recordset
传回值的区别:
output和return都可在批次程式中用变量接收,而recordset则传回到执行批次的客户端中。
实例3:使用带有复杂 select 语句的简单过程
下面的存储过程从四个表的联接中返回所有作者(提供了姓名)、出版的书籍以及出版社。该存储过程不使用任何参数。
use pubs
if exists (select name from sysobjects
where name = 'au_info_all' and type = 'p')
drop procedure au_info_all
go
create procedure au_info_all
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
go
au_info_all 存储过程可以通过以下方法执行:
execute au_info_all
-- or
exec au_info_all
如果该过程是批处理中的第一条语句,则可使用:
au_info_all
实例4:使用带有参数的简单过程
create procedure au_info
@lastname varchar(40),
@firstname varchar(20)
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
where au_fname = @firstname
and au_lname = @lastname
go
au_info 存储过程可以通过以下方法执行:
execute au_info 'dull', 'ann'
-- or
execute au_info @lastname = 'dull', @firstname = 'ann'
-- or
execute au_info @firstname = 'ann', @lastname = 'dull'
-- or
exec au_info 'dull', 'ann'
-- or
exec au_info @lastname = 'dull', @firstname = 'ann'
-- or
exec au_info @firstname = 'ann', @lastname = 'dull'
如果该过程是批处理中的第一条语句,则可使用:
au_info 'dull', 'ann'
-- or
au_info @lastname = 'dull', @firstname = 'ann'
-- or
au_info @firstname = 'ann', @lastname = 'dull'
实例5:使用带有通配符参数的简单过程
create procedure au_info2
@lastname varchar(30) = 'd%',
@firstname varchar(18) = '%'
as
select au_lname, au_fname, title, pub_name
from authors a inner join titleauthor ta
on a.au_id = ta.au_id inner join titles t
on t.title_id = ta.title_id inner join publishers p
on t.pub_id = p.pub_id
where au_fname like @firstname
and au_lname like @lastname
go
au_info2 存储过程可以用多种组合执行。下面只列出了部分组合:
execute au_info2
-- or
execute au_info2 'wh%'
-- or
execute au_info2 @firstname = 'a%'
-- or
execute au_info2 '[ck]ars[oe]n'
-- or
execute au_info2 'hunter', 'sheryl'
-- or
execute au_info2 'h%', 's%'
= 'proc2'
实例6:if...else
存储过程,其中@case作为执行update的选择依据,用if...else实现执行时根据传入的参数执行不同的修改.
--下面是if……else的存储过程:
if exists (select 1 from sysobjects where name = 'student' and type ='u' )
drop table student
go
if exists (select 1 from sysobjects where name = 'spupdatestudent' and type ='p' )
drop proc spupdatestudent
go
create table student
(
fname nvarchar (10),
fage
smallint ,
fdiqu varchar (50),
ftel int
)
go
insert into student values ('x.x.y' , 28, 'tesing' , 888888)
go
create proc spupdatestudent
(
@fcase int ,
@fname nvarchar (10),
@fage smallint ,
@fdiqu varchar (50),
@ftel int
)
as
update student
set fage = @fage, -- 传 1,2,3 都要更新 fage 不需要用 case
fdiqu = (case when @fcase = 2 or @fcase = 3 then @fdiqu else fdiqu end ),
ftel = (case when @fcase = 3 then @ftel else ftel end )
where fname = @fname
select * from student
go
-- 只改 age
exec spupdatestudent
@fcase = 1,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101
-- 改 age 和 diqu
exec spupdatestudent
@fcase = 2,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101
-- 全改
exec spupdatestudent
@fcase = 3,
@fname = n'x.x.y' ,
@fage = 80,
@fdiqu = n'update' ,
@ftel = 1010101