SQL学习和积累(一些不常用确很有用的方法)

1、 (1判断指定存储过程是否存在
        if exists(select * from sysobjects where name='存储过程名' and type='P')
  drop proc '存储过程名'
    (2判断指定表的列是否存在
 if not exists (select   *   from   syscolumns   where   id=(select   id   from   sysobjects   where
    name='表名')   and   name='列名')
    (3判断指定表是否存在
        if exists(select * from sysobjects where name='表名' and type='U')
  drop table '表名'
    (4找到指定表指定列的类型
 select name from systypes where xtype in (select xtype from syscolumns where id in (select id from sysobjects where name='表名') and name='列名')
2、通过参数修改和删除表的列名
 /*修改列名*/
  declare @sql varchar(100)
  set @sql='Alter table Student add ['+@value3+'] varchar(50) NULL' --@value3为列名
 exec (@sql)
 /*删除指定列名*/
 set @sql='alter table Student drop column ['+@datapropertyname+']'
 exec (@sql)
3、去除重复值
        select distinct tableName  from InitDataGridView
4、sql动态语句中的参数传递
 ALTER  proc pr_UpdateInitDataGridView1
 @value1 int,   --GuId
 @value2 varchar(50),  --columnName
 @value3 bit  --value
 as
 declare @sql nvarchar(500),@param nvarchar(500)
 set @sql=N'update InitDataGridView set ['+@value2+']=@value3 where GuID=@value1'
 set @param=N'@value1 int,@value2 varchar(50),@value3 bit'       --必须指定参数
 execute sp_executesql @sql,@param,@value1,@value2,@value3 --使用存储过程执行带有参数的sql语句

 execute pr_UpdateInitDataGridView1  @value1,@value2,@value3
5、exec执行带返回值的存储过程的情况

我们来看一个简单的存储过程:

create procedure ProTest
(
         @name varchar(10),
         @money int output
)
as
begin
        if(@name='1')
                  set @money=1000
        else
                  set @money=2000
end
这个只是一个简单的示例,这个存储过程返回的是@money 这个参数的值,那么当我们在另外一个存储过程中调用此存储过程的时候如何获取这个参数呢,方法如下:

declare @m int ---用来接收返回值的变量
exec ProTest @name='1',@money=@m output --一定要注名是output
就这么简单,我们就获得了返回值,然后就可以利用它了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值