473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dynamic sql - how to use 'if exists' with variable tables..?

Hi all

In the SP below im (trying to) do some dynamic sql. As you can see the table
to use is set as a variable and the 'exec' method used to run the
sqlstatements.
My problem is that the 'if exists' method is not doing what i was hoping it
could do.
The @presql command returns somewhere between 0 or 50 rows (give and take) -
i just want the 'if exists' part to determine if the select statement
returns something or not since i then will have to update a current row - or
insert a new one.
Even if there is no rows returned, the 'if exists' command will return true
:-/

Any suggestions to a different way of approach...?

Thanks in advance :-)

######## Stored procedure start ########
[various @ variables]
....
declare @presql varchar(200)
select @presql = 'SELECT * FROM '+@CurrentDB+' where btsiteID='+
cast(@SiteID as varchar(6))+''

IF exists((@presql))
BEGIN
declare @UpdateSQL varchar(400)
set @UpdateSQL = 'UPDATE '+@CurrentDB+' SET btDate='''+cast(@FileDate as
varchar(12))+''''
exec(@UpdateSQL)
END

ELSE

BEGIN
declare @InsertSQL varchar(2000)

select @InsertSQL = 'INSERT INTO ' + @CurrentDB + '
(btDate,
btTime)
VALUES
('''+ cast(@FileDate as varchar(12)) + ''','
+ cast(@ImportTime as varchar(6)) + ')'

EXEC(@InsertSQL)
END
######## Stored procedure end ########
Jul 20 '05 #1
2 22037
oj
If exists(@sql) does not actually execute @sql and then evaluates the
resultset. What you want to do is this:

declare @presql varchar(4000)
select @presql = '
IF EXISTS(SELECT * FROM '+@CurrentDB+' where btsiteID='+cast(@SiteID as
varchar(6))+')
BEGIN
UPDATE '+@CurrentDB+' SET btDate='''+cast(@FileDate as varchar(12))+'''
END
ELSE
BEGIN
INSERT INTO ' + @CurrentDB + '(btDate, btTime) VALUES ('''+ cast(@FileDate
as varchar(12)) + ''',' + cast(@ImportTime as varchar(6)) + ')
END
'
exec sp_executesql @presql
"Bane" <ba**@noname.net> wrote in message
news:p%********************@news000.worldonline.dk ...
Hi all

In the SP below im (trying to) do some dynamic sql. As you can see the
table
to use is set as a variable and the 'exec' method used to run the
sqlstatements.
My problem is that the 'if exists' method is not doing what i was hoping
it
could do.
The @presql command returns somewhere between 0 or 50 rows (give and
take) -
i just want the 'if exists' part to determine if the select statement
returns something or not since i then will have to update a current row -
or
insert a new one.
Even if there is no rows returned, the 'if exists' command will return
true
:-/

Any suggestions to a different way of approach...?

Thanks in advance :-)

######## Stored procedure start ########
[various @ variables]
...
declare @presql varchar(200)
select @presql = 'SELECT * FROM '+@CurrentDB+' where btsiteID='+
cast(@SiteID as varchar(6))+''

IF exists((@presql))
BEGIN
declare @UpdateSQL varchar(400)
set @UpdateSQL = 'UPDATE '+@CurrentDB+' SET btDate='''+cast(@FileDate as
varchar(12))+''''
exec(@UpdateSQL)
END

ELSE

BEGIN
declare @InsertSQL varchar(2000)

select @InsertSQL = 'INSERT INTO ' + @CurrentDB + '
(btDate,
btTime)
VALUES
('''+ cast(@FileDate as varchar(12)) + ''','
+ cast(@ImportTime as varchar(6)) + ')'

EXEC(@InsertSQL)
END
######## Stored procedure end ########

Jul 20 '05 #2
This looks promising :-)

Thanks a bunch!
"oj" <no**********@home.com> wrote:
If exists(@sql) does not actually execute @sql and then evaluates the
resultset. What you want to do is this:

declare @presql varchar(4000)
select @presql = '
IF EXISTS(SELECT * FROM '+@CurrentDB+' where btsiteID='+cast(@SiteID as
varchar(6))+')
BEGIN
UPDATE '+@CurrentDB+' SET btDate='''+cast(@FileDate as varchar(12))+'''
END
ELSE
BEGIN
INSERT INTO ' + @CurrentDB + '(btDate, btTime) VALUES ('''+ cast(@FileDate
as varchar(12)) + ''',' + cast(@ImportTime as varchar(6)) + ')
END
'
exec sp_executesql @presql
"Bane" <ba**@noname.net> wrote in message
news:p%********************@news000.worldonline.dk ...
Hi all

In the SP below im (trying to) do some dynamic sql. As you can see the
table
to use is set as a variable and the 'exec' method used to run the
sqlstatements.
My problem is that the 'if exists' method is not doing what i was hoping
it
could do.
The @presql command returns somewhere between 0 or 50 rows (give and
take) -
i just want the 'if exists' part to determine if the select statement
returns something or not since i then will have to update a current row - or
insert a new one.
Even if there is no rows returned, the 'if exists' command will return
true
:-/

Any suggestions to a different way of approach...?

Thanks in advance :-)

######## Stored procedure start ########
[various @ variables]
...
declare @presql varchar(200)
select @presql = 'SELECT * FROM '+@CurrentDB+' where btsiteID='+
cast(@SiteID as varchar(6))+''

IF exists((@presql))
BEGIN
declare @UpdateSQL varchar(400)
set @UpdateSQL = 'UPDATE '+@CurrentDB+' SET btDate='''+cast(@FileDate as varchar(12))+''''
exec(@UpdateSQL)
END

ELSE

BEGIN
declare @InsertSQL varchar(2000)

select @InsertSQL = 'INSERT INTO ' + @CurrentDB + '
(btDate,
btTime)
VALUES
('''+ cast(@FileDate as varchar(12)) + ''','
+ cast(@ImportTime as varchar(6)) + ')'

EXEC(@InsertSQL)
END
######## Stored procedure end ########


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
2
by: znelson | last post by:
I'm looking for a way to transform the contents of n source tables into a single destination table. This by itself is no problem. However, the name of the source tables change, so I'll need to...
9
by: Tom | last post by:
What I mean is why can I only allocate const size stuff on the stack in C++? If I want to allocate a variable amount I need to use the OS API (Win32 in my case). Thanks, Tom.
31
by: NickName | last post by:
/* goal: dynamic evaluation of table row platform: sql 2000 */ use northwind; declare @tbl sysname set @tbl = 'customers' EXEC('select count(*) from ' +@tbl)
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
6
by: Vmusic | last post by:
Hi, I am using Javascript to add rows to tables, etc. in a function I am calling. I pass the function the ID of the div, and what I want in the rows, and it will add rows to a table in the div. ...
16
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
0
by: mix01 | last post by:
Hi, I am trying to get some VBA code working, but am preplex as to why it does not work. I would really appreciate any level of help. Many thanks, Mix01 Version of the program
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.