473,799 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL IF...ELSE syntax

3 New Member
I'm trying to create an if/else case in a stored procedure where if te record is not found, it returns blank values, and if it is found I get the real values. But when I try to execute the SP update I get column does not exist for the else clause (all columns starting with shift through sequence)

I'm sure this is a simple problem, and I apologize in advance.

Expand|Select|Wrap|Line Numbers
  1. USE [ADC]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[getSchedule]    Script Date: 10/20/2008 11:54:55 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:        Jeffrey Grace
  10. -- Create date: 2008-10-16
  11. -- Description:    Gets scheduled data and returns to PLC
  12. -- =============================================
  13. ALTER PROCEDURE [dbo].[getSchedule]
  14.     -- Input
  15.     @facility char(2),
  16.     @line int,
  17.     @shiftin int,
  18.  
  19.     --@sequence int,
  20.     -- Output
  21.     @shiftout int output,    
  22.     @sequence int output,    
  23.     @operator_num char(8) output,
  24.     @operator_name char(30) output,
  25.     @pack_num char(8) output,
  26.     @pack_name char(30) output,
  27.     @item1 char(15) output,
  28.     @item2 char(15) output,
  29.     @item3 char(15) output,
  30.     @item4 char(15) output,
  31.     @cutQty char(10) output,
  32.     @cutTime char(4) output
  33.  
  34. AS
  35. BEGIN
  36.     SET NOCOUNT ON;
  37.     select top 1    shift, operator, pack, item1,
  38.         item2, item3, item4, cutQty, cutTime, sequence
  39.         from schedule where 
  40.             facility=@facility and
  41.             line=@line and
  42.             shift=@shiftin 
  43.  
  44.     if @@ROWCOUNT = 0
  45.     BEGIN
  46.         set @shiftout=0
  47.         set    @sequence=0
  48.         set    @operator_num=''
  49.         set    @operator_name=''
  50.         set    @pack_num=''
  51.         set    @pack_name=''
  52.         set    @item1=''
  53.         set    @item2=''
  54.         set    @item3=''
  55.         set    @item4=''
  56.         set    @cutQty=''
  57.         set    @cutTime=''
  58.     End
  59.  
  60.     Else
  61.     BEGIN        
  62.         set        @shiftout=shift
  63.         set        @operator_num = operator
  64.         set        @pack_num = pack
  65.         set        @item1 = item1
  66.         set        @item2 = item2
  67.         set        @item3 = item3
  68.         set        @item4 = item4
  69.         set        @cutQty = cutQty
  70.         set        @cutTime = cutTime
  71.         set        @sequence = sequence
  72.  
  73.         select @operator_name = emp_name from operators where 
  74.             emp_num=@operator_num
  75.  
  76.         select @pack_name = emp_name from operators where 
  77.             emp_num=@pack_num
  78.     END
  79. END
Oct 20 '08 #1
1 10613
ck9663
2,878 Recognized Expert Specialist
SQL Server does not always work like DBF or recordset processing ;)

Try the following.....I did not test this for any error but you should get the idea of what I'm trying to say. Also, depending on your requirement, change the type of JOIN as necessary.


Expand|Select|Wrap|Line Numbers
  1. USE [ADC]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[getSchedule]    Script Date: 10/20/2008 11:54:55 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. -- =============================================
  9. -- Author:        Jeffrey Grace
  10. -- Create date: 2008-10-16
  11. -- Description:    Gets scheduled data and returns to PLC
  12. -- =============================================
  13. ALTER PROCEDURE [dbo].[getSchedule]
  14.     -- Input
  15.     @facility char(2),
  16.     @line int,
  17.     @shiftin int,
  18.  
  19.     --@sequence int,
  20.     -- Output
  21.     @shiftout int output,    
  22.     @sequence int output,    
  23.     @operator_num char(8) output,
  24.     @operator_name char(30) output,
  25.     @pack_num char(8) output,
  26.     @pack_name char(30) output,
  27.     @item1 char(15) output,
  28.     @item2 char(15) output,
  29.     @item3 char(15) output,
  30.     @item4 char(15) output,
  31.     @cutQty char(10) output,
  32.     @cutTime char(4) output
  33.  
  34. AS
  35. BEGIN
  36.     SET NOCOUNT ON;
  37.  
  38.     select top 1 @shiftout=shift, @operator_num = operator, @pack_num = pack, @item1 = item1,
  39.         @item2 = item2, @item3 = item3, @item4 = item4, @cutQty = cutQty, @cutTime = cutTime, @sequence = sequence,
  40.         @operator_name = op_name.emp_name, @pack_name = pk_name.emp_name
  41.         from schedule s
  42.     left join operators op_name on emp_num = s.operator
  43.         left join operators pk_name on emp_num = s.pack
  44.         where facility=@facility and line=@line andshift=@shiftin 
  45.  
  46.     if @@ROWCOUNT = 0
  47.         select @shiftout=0, @sequence=0, @operator_num='',@operator_name='',@pack_num='',@pack_name='',@item1='',@item2='',@item3='',@item4='',@cutQty='', @cutTime=''
  48. END
  49.  
  50.  
Happy coding!

-- CK
Oct 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
1861
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting great benefit from it? What does ELSE minor-mode for Emacs do that is so great for you? Chris
44
3382
by: seberino | last post by:
Tuples are defined with regards to parentheses ()'s as everyone knows. This causes confusion for 1 item tuples since (5) can be interpreted as a tuple OR as the number 5 in a mathematical expression such as x = (5) * (4+6). Wouldn't it have been better to define tuples with <>'s or {}'s or something else to avoid this confusion?? Perhaps ()'s are a good idea for some other reason I don't know?
27
3081
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a few weeks ago while trying to find the best way to form a if-elif-else block, that on a very general level, an 'also' statement might be useful. So I was wondering what others would think of it.
2
5571
by: phaser2001 | last post by:
Hi all, I have the below user-defined function on mssql 2000 and I can't work out why i'm getting the following error: ----- Server: Msg 156, Level 15, State 1, Procedure fnCalculateOutworkerPaymentForBox, Line 15 Incorrect syntax near the keyword 'IF'. Server: Msg 170, Level 15, State 1, Procedure fnCalculateOutworkerPaymentForBox, Line 23
5
1994
by: juglesh | last post by:
"$string = isset($xyz) ? $xyz : "something else";" Hello, someone gave code like this in another thread. I understand (by inference) what it does, but have not found any documentation on this type of syntax. Any one have links to this shortuct(?) syntax and other types of syntax? thanks
63
4807
by: Jake Barnes | last post by:
In the course of my research I stumbled upon this article by Alex Russel and Tim Scarfe: http://www.developer-x.com/content/innerhtml/default.html The case is made that innerHTML should never be used. I'm wondering, If I wanted all the content of BODY as a string, how else could I get except through innerHTML?
8
2817
by: Jim Michaels | last post by:
C:\prj\quiz\withusers>php tareports.php PHP Parse error: syntax error, unexpected T_ELSE in C:\prj\quiz\withusers\tareports.php on line 205 this is the section of code. if (isset($row4)) { if (isset($row5)) { //answer given? if ($row4==$row5) {
0
808
by: stef mientki | last post by:
hello, I've the idea that the "else" syntax is not always handled correctly, or I'm overlooking something. This pieces of code are automatic translation from another language, sometimes it works, sometimes it doesn't ??? This piece of code works nice:
8
1287
by: callumagus | last post by:
Python Version 2.0 Windows Vista (I know, I know...) Im very new to python and have only been doing it for two weeks. print 'Type in lowercase!'; String_Var = raw_input ('Add, Divide, Multiply or Subtract? '); if 'add' in String_Var:
19
1863
by: creative1 | last post by:
hi I am in strange problem. I have javascript for for validation. If I have any entry invalid it prompts me to corrrect it. All checks I have works fine. However, form does not response when there is no valid entry. Here is my code. What do I do? function validate(form) { if ((form.clientName.value.length==0)||(form.clientName.value.length<3) ||(form.clientName.value.length>50)) { mesg="You have entered...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.