CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

SQL语句转存储过程

楼主caicai_45(菜菜)2005-05-25 07:24:13 在 .NET技术 / ASP.NET 提问

drop     table     classname      
  declare     @TeacherID     int      
  declare     @a     char(50)      
  declare     @b     char(50)      
  declare     @c     char(50)      
  declare     @d     char(50)      
  declare     @e     char(50)      
  set     @TeacherID=1      
     
  select     @a=DRClass1,     @b=DRClass2,     @c=DRClass3,     @d=DRClass4,     @e=DRClass5     from     Teacher     Where     TeacherID     =     @TeacherID      
     
  create     table         classname(classname     char(50))      
  insert     into     classname         (classname)     values     (@a)      
  if     (@b     is     not     null)          
  begin      
  insert     into     classname         (classname)     values     (@b)      
     
        if     (@c     is     not     null)      
        begin      
        insert     into     classname         (classname)     values     (@c)      
     
                if     (@d     is     not     null)          
                begin      
                insert     into     classname         (classname)     values     (@d)      
                        if     (@e     is     not     null)          
                        begin      
                        insert     into     classname         (classname)     values     (@e)      
                        end      
                end      
        end      
  end      
     
  select     *     from     classname      
     
  以上这些SQL语句能不能转成一个存储过程?我自己试了下      
  ALTER     PROCEDURE     Pr_GetClass      
     
  @TeacherID     int,      
  @a     char(50),      
  @b     char(50),      
  @c     char(50),      
  @d     char(50),      
  @e     char(50)      
  as      
     
  select     @a=DRClass1,     @b=DRClass2,     @c=DRClass3,     @d=DRClass4,     @e=DRClass5     from     Teacher     Where     TeacherID     =     @TeacherID      
  DROP     TABLE     classname      
  create     table         classname(classname     char(50))      
     
  insert     into     classname         (classname)     values     (@a)      
  if     (@b     is     not     null)          
  begin      
  insert     into     classname         (classname)     values     (@b)      
     
        if     (@c     is     not     null)      
        begin      
        insert     into     classname         (classname)     values     (@c)      
     
                if     (@d     is     not     null)          
                begin      
                insert     into     classname         (classname)     values     (@d)      
                        if     (@e     is     not     null)          
                        begin      
                        insert     into     classname         (classname)     values     (@e)      
                        end      
                end      
        end      
  end      
     
  select     *     from     classname      
  但是这样的话,这个存储过程就有6个变量,实际上应该只提供一个变量就可以了      
  有什么办法吗?谢谢了 问题点数:20、回复次数:8Top

1 楼singlepine(小山)回复于 2005-05-25 07:56:42 得分 0

是需要六个参数的,要不然你的@TeacherID,@a,@b,@c,@d,@e这些值将怎么知道呢    
  create     PROCEDURE     Pr_GetClass        
  @TeacherID     int,      
  @a     char(50),      
  @b     char(50),      
  @c     char(50),      
  @d     char(50),      
  @e     char(50)      
  as      
     
  select     @a=DRClass1,     @b=DRClass2,     @c=DRClass3,     @d=DRClass4,     @e=DRClass5     from     Teacher     Where     TeacherID     =     @TeacherID      
  DROP     TABLE     classname      
  create     table         classname(classname     char(50))      
     
  insert     into     classname         (classname)     values     (@a)      
  if     (@b     is     not     null)          
  begin      
  insert     into     classname         (classname)     values     (@b)      
     
        if     (@c     is     not     null)      
        begin      
        insert     into     classname         (classname)     values     (@c)      
     
                if     (@d     is     not     null)          
                begin      
                insert     into     classname         (classname)     values     (@d)      
                        if     (@e     is     not     null)          
                        begin      
                        insert     into     classname         (classname)     values     (@e)      
                        end      
                end      
        end      
  end        
  select     *     from     classname    
  goTop

2 楼colinliu(流浪人)回复于 2005-05-25 08:05:01 得分 0

同意楼上Top

3 楼joephoenix(迎风的狼)回复于 2005-05-25 08:42:33 得分 0

变量没什么问题吧!Top

4 楼renyu732(Sysinfo)回复于 2005-05-25 08:43:18 得分 0

StudyTop

5 楼xzq686(★_瞬_★)回复于 2005-05-25 08:57:05 得分 20

 
  存储过程的参数只要一个就行。  
   
  那几个是在存储过程中定义的局部参数。因为调用存储过程的时候只有一个输入参数@TeacherID  
   
  create     PROCEDURE     Pr_GetClass        
  @TeacherID     int  
  as      
   
  Declare   @a     char(50),   @b     char(50),   @c     char(50),   @d     char(50),   @e     char(50)      
  select     @a=DRClass1,     @b=DRClass2,     @c=DRClass3,     @d=DRClass4,     @e=DRClass5     from     Teacher     Where     TeacherID     =     @TeacherID      
  DROP     TABLE     classname      
  create     table         classname(classname     char(50))      
  insert     into     classname         (classname)     values     (@a)      
  if     (@b     is     not     null)          
  begin      
  insert     into     classname         (classname)     values     (@b)      
  if     (@c     is     not     null)      
        begin      
                insert     into     classname         (classname)     values     (@c)      
                if     (@d     is     not     null)          
                begin      
                        insert     into     classname         (classname)     values     (@d)      
                        if     (@e     is     not     null)          
                        begin      
                              insert     into     classname         (classname)     values     (@e)      
                        end      
                end      
        end      
  end        
  select     *     from     classname    
  go  
  Top

6 楼yishan116(哈哈)回复于 2005-05-25 08:57:52 得分 0

singlepine(小山)    
  我看没问题,支持Top

7 楼cabxyz(cab)回复于 2005-05-25 09:09:10 得分 0

去数据库区Top

8 楼liuqm(blue)回复于 2005-05-25 09:27:39 得分 0

试试就知道,Top

相关问题

  • 用存储过程还是 sql 语句 ?
  • 求一SQL语句或存储过程
  • 求一SQL语句或存储过程。
  • 求一SQL语句,或存储过程
  • c#中的sql语句转换成存储过程的新问题!!急等!
  • Oracle的存储过程如何转成SQL Server存储过程
  • 请问有没有办法将这句SQL语句转为存储过程(SQL SERVER7)
  • SQL Server的存储过程怎么执行sql语句
  • SQL语句让存储过程生成SQL脚本!
  • 将c#sql语句转换成存储过程,有问题!!帮解决!!急迫等!!

关键词

  • 存储过程
  • null
  • drclass
  • classname
  • teacherid
  • begin insert
  • 参数
  • getclass
  • declare
  • drop

得分解答快速导航

  • 帖主:caicai_45
  • xzq686

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo