473,692 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7822
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.i n (Arijit Chatterjee) wrote in message news:<ea******* *************** ****@posting.go ogle.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.goo gle.com...
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),co lumn2
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.bt internet.com>.. .
See inline

"Arijit Chatterjee" <ar************ *****@yahoo.co. in> wrote in message
news:ea******** *************** ***@posting.goo gle.com...
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
5325
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 records 1 : connection.execute "delete from X record 1" 2. The procedure adds record 1 again using the recordset object.
1
15477
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 & customer_number, customer_name, customer_address, customer_city, customer_state, customer_zip, customer_phone, vin, acct_number, tag, title_number, tag_type_number, branch_number, institution_number, yr_make, state_from, type_number)...
2
13613
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 * from Employee;" <% Dim objRS, sqlStmt set objRS = Server.CreateObject("ADODB.Recordset") Dim conn Set conn = Server.CreateObject("ADODB.Connection")
6
5386
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 as SQL Server allows it as the exemple bellow shows : EXECUTE 'SELECT * FROM Test' Is there any way to do so with DB2 ?
2
6750
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 STATEMENT; DECLARE stmt2 STATEMENT;
5
8857
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 the table already exists! (Error 3010 Table already exists). But I made sure that the table were dropped before I executed the statement and I went into the database to check that the table was deleted before I ran the "execute" statement. Here...
1
2937
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 written a few stored procedures but havent used prepare and execute.
4
10681
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 generator to generate the SQL statement. Debugging points to this line: dbs.Execute strSQl, dbFailOnError Here's the code:
4
2826
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 CurrentProject = CurrentDB() 'CurrentProject.Execute "Insert into TransactionHistory values(FirstName, LastName, LastPaymentDate, LastPaymentAmount)", dbFailOnError
3
6956
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 me one row. Further, i need to capture the result of the execute Immediate into a
0
8600
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
8535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9079
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...
1
8794
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6458
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5817
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4555
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2968
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
2234
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.