473,394 Members | 1,794 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,394 software developers and data experts.

Execute Statement..

Dear Friens,
I am writing a SP.But storeing a query in a variable.But at the time
of execution generating error.Exam
===================
Declare @query varchar{500)
Set @query = 'Select * from table'

if exists (exec (@query))
print 'Hi'
====================
But "if exists" line giving error.How do I solve this.Please help me
out.
Reagrds
Arijit Chatterjee
Jul 20 '05 #1
4 7809
Hi

EXISTS requires a sub-query as the test, and EXEC does not do that.

If you have to do this dynamically then you may want to look at
sp_executesql to return a count or move everything into @query.

If you posted more precise detail it may be easier to offer advice.

John
ar*****************@yahoo.co.in (Arijit Chatterjee) wrote in message news:<ea**************************@posting.google. com>...
Dear Friens,
I am writing a SP.But storeing a query in a variable.But at the time
of execution generating error.Exam
===================
Declare @query varchar{500)
Set @query = 'Select * from table'

if exists (exec (@query))
print 'Hi'
====================
But "if exists" line giving error.How do I solve this.Please help me
out.
Reagrds
Arijit Chatterjee

Jul 20 '05 #2
Create proc sp_test
as
Declare @query varchar{500)
Declare @var varchar(10)
set @var='Test'
Set @query = 'Select * from table' + ' Where ' + 'Colname = ' + @var
exec (@query)----> Working fine
if exists (exec (@query))----> Sending error
print 'Hi'
Now tell me how to solve this.
Regards
Arijit Chatterjee
Jul 20 '05 #3
See inline

"Arijit Chatterjee" <ar*****************@yahoo.co.in> wrote in message
news:ea**************************@posting.google.c om...
Create proc sp_test
as
Declare @query varchar{500) This will not compile
Declare @var varchar(10)
set @var='Test'
Set @query = 'Select * from table' + ' Where ' + 'Colname = ' + @var If you were doing this correctly the value in @var would be enquoted
exec (@query)----> Working fine
if exists (exec (@query))----> Sending error
print 'Hi'
Now tell me how to solve this.
Reading books online would be the first place to look.

Then read http://www.algonet.se/~sommar/dynamic_sql.html on why you should
justify the use of dynamic SQL.

You should then be able to work out how to return the count using
sp_executesql to get what is require.
Regards
Arijit Chatterjee


John
Jul 20 '05 #4
What you need to do in this case is create a sql server temporary
table (such as create table #temp (column1 nvarchar(10),column2
nvarchar(10))) and then build your @query string so that it inserts
the results of your dynamic select statement into the temp table. You
can then play the whole "exists" game on the results of a query
against your #temp table.

Make sure that you do not build your temp table dynamically. And
remember, you can't insert results from a dynamically built select
statement into a normal variable, but you can insert them into a temp
table.

Good Luck!

"John Bell" <jb************@hotmail.com> wrote in message news:<bo**********@hercules.btinternet.com>...
See inline

"Arijit Chatterjee" <ar*****************@yahoo.co.in> wrote in message
news:ea**************************@posting.google.c om...
Create proc sp_test
as
Declare @query varchar{500)

This will not compile
Declare @var varchar(10)
set @var='Test'
Set @query = 'Select * from table' + ' Where ' + 'Colname = ' + @var

If you were doing this correctly the value in @var would be enquoted
exec (@query)----> Working fine
if exists (exec (@query))----> Sending error
print 'Hi'
Now tell me how to solve this.


Reading books online would be the first place to look.

Then read http://www.algonet.se/~sommar/dynamic_sql.html on why you should
justify the use of dynamic SQL.

You should then be able to work out how to return the count using
sp_executesql to get what is require.
Regards
Arijit Chatterjee


John

Jul 20 '05 #5

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

Similar topics

4
by: Jill Graham | last post by:
Hi, I'm using an access database and found following problem : I have a table X with 2 records : record 1 & record 2. I execute following procedure with 2 steps : 1. The procedure deletes...
1
by: PJ | last post by:
<% Set Conn = Server.CreateObject("ADODB.Connection") Conn.open application("dtat_motor_connectionstring") set rs = new adodb.recordset 'Set RS = Conn.Execute(' "exec spcn_update_transactions &...
2
by: Matt | last post by:
I want to exexute stored procedure in ASP, but it has error "Microsoft VBScript compilation (0x800A0401) Expected end of statement" on line (1). The stored procedure "sp_emp" contain "select *...
6
by: Doohan W. | last post by:
Hi, I'm now working with DB2, and I can't find out how to execute the contents of a string Statement, without using a Java/... procedure, only using SQL statements. I know that some SQBDs such...
2
by: Nabil | last post by:
I am new to DB2 and here is my situation. I have 2 temp tables where I am trying to insert data (from 2 select statements) DECLARE v_t1 VARCHAR(50); DECLARE v_t1 VARCHAR(50); DECLARE stmt1...
5
by: Andrew | last post by:
Hi: I run into the following problem where I would use CreateQuerydef to create a new Table and then use "Execute" to execute the statement. However, I keep getting an error message saying that...
1
by: rAinDeEr | last post by:
Hi, I have started using DB2 UDB ESE v8.2 in Linux recently. I have seen a lot of articles on packages, prepare statement, bind and execute statement. I havent got a clear idea. I have...
4
by: HeislerKurt | last post by:
I'm getting the infamous error, "Too few parameters. Expected 2", when executing an update SQL statement in VBA. I assume it's a SQL syntax issue, but I can't find the problem, and I used a VBA...
4
by: gmazza | last post by:
Hi there, I am trying to insert 4 fields into a table grabbing the 4 fields from a form, here is my code: Private Sub LastPaymentAmount_AfterUpdate() Dim CurrentProject As DAO.Database Set...
3
by: Rahul Babbar | last post by:
Hi, I have the following doubt. Suppose I use the execute immediate statement and the statement to be executed is a Select statement from the sysibm.sysdummy1 table which will always return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.