Reputation: 1116
I have a problem with entering an if statement in SQL Server.
I tried wrapping the if inner statements in BEGIN END blocks but no luck.
if (CHARINDEX(',', @Configs, 0) = 0)
print 'Single config detected'
begin
if (@configAlreadyExists = 0)
begin
print 'Cofnig ' + @Configs + ' isn''t in the Health Check config display list, checking if it exists in table...'
if not exists (select 1 from table where ConfigKey = @Configs)
begin
print 'Cofnig ' + @Configs + ' doesn''t exist in table, add the config first.'
end
else
begin
print 'Cofnig ' + @Config + ' exists in table, adding config for Health Check display.'
end
end
end
If I enter any string I would expect to eventually see the exists or doesn't exist in table printout, but I don't.
The only printout I see is 'Single config detected'.
Upvotes: 0
Views: 292
Reputation: 2862
The backticks suggest one of two things - an error during posting or use of a database engine that is not sql server. Assume the former but many posters don't know what database engine they use.
Your first "begin" is likely not correctly placed. You probably meant to do:
if charindex() = 0
begin
print ...
if @configAlreadyExists = 0
begin
...
end
end
As it stands right now, a little effort at formatting makes the flow more obvious
if (CHARINDEX(',', @Configs, 0) = 0)
print 'Single config detected'
begin
if (@configAlreadyExists = 0)
begin
print 'Cofnig ' + @Configs + ' isn''t in the Health Check config display list, checking if it exists in table...'
if not exists (select 1 from table where ConfigKey = @Configs)
print 'Cofnig ' + @Configs + ' doesn''t exist in table, add the config first.'
else
print 'Cofnig ' + @Config + ' exists in table, adding config for Health Check display.'
end
end
I removed the inner-most (and not needed) begin/end statements to make things more clear and more compact.
Upvotes: 2