85348

getting error for if/else in a stored procedure

I am relatively very new to SQL queries but I have this stored procedure where I am trying to get value of a declared variable as mentioned below but getting error,

First line is 20 here,

declare @m_ID_v int set @m_ID_v = ( select ID_C from M_T where MName_C = @MName_parameter) declare @g bit if (select G_L_Column from G_L_table Where M_ID_Column = @M_ID_variable) set @g_v = 1 else set @g_variable = 0

Exception I get:

Msg 4145, Level 15, State 1, Procedure GetID, Line 20 An expression of non-boolean type specified in a context where a condition is expected, near 'set'. Msg 156, Level 15, State 1, Procedure GetID, Line 21 Incorrect syntax near the keyword 'else'.

Now if I remove declare @g... and try to parse it, no error occurs

<strong>EDIT</strong>

I want my code to check for returned value by my select statement so "if exists" is not really what am looking for, sorry.

Answer1:

declare @m_ID_v int set @m_ID_v = ( select ID_C from M_T where MName_C = @MName_parameter) declare @g bit if ((select G_L_Column from G_L_table Where M_ID_Column = @M_ID_variable) = value ) set @g_v = 1 else set @g_variable = 0

Answer2:

try use if exists:

declare @g_v bit if exists(select G_L_Column from G_L_table Where M_ID_Column = @M_ID_variable) set @g_v = 1 else set @g_v = 0

Answer3:

You can't say

if (select ...

You have to compare something with something else in a if statement, or use a boolean function such as exists

Recommend

  • SQL case when set variable
  • GROUP_CONCAT mysql statement error
  • For loop with if condition on multiple R functions
  • netsh acl setting (need alternative method - registry settings?)
  • How to get data from **Realm database** using **date object**?
  • Multiple producers single consumer locking schema
  • How to print columns containing value
  • Updating both a ConcurrentHashMap and an AtomicInteger safely
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • How do I superscript characters in a UIButton?
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Blackberry - Custom EditField Cursor
  • Textfile Structure (tables)
  • Rails Find when some params will be blank
  • How do I open a C file with a relative path?
  • How to set ini file attributes during an Inno install
  • Test if a set exists before trying to drop it
  • Cancel a live stream “fast motion” catch-up in Flash
  • Algorithm for a smudge tool?
  • Get one-time binding to work for ng-if
  • How do I change content of ComboFieldEditor?
  • Adding a button at the bottom of a table view
  • How can I send an e-mail from a vbs script
  • Spring security and special characters
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • How do I fake an specific browser client when using Java's Net library?
  • Date difference with leap year
  • angularjs unit test when to use $rootScope.$new()
  • Volusion's generic SQL folder, functionality
  • Menu Color Fade on Hover with Jquery
  • How to limit post in wp_query
  • Hazelcast - OperationTimeoutException
  • How to make Safari send if-modified-since header?
  • Adding custom controls to a full screen movie
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory