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

using exec

I need to use exec command and it is working fine but there is some problem in query. Can you please help me in building query.

I am passing id and table name to stored procedure.



Following will be the form of query after merging id and table name-

SELECT *
FROM dbname. BillGeneralTaxDetails_SC_07_01,dbname. BCollRateFile
WHERE (dbname. BillGeneralTaxDetails_SC_07_01.id like '%' + id + '%' or id is NULL)
and (dbname. BillGeneralTaxDetails_SC_07_01.TaxRollYr = dbname. BCollRateFile.TaxRollYr) and (dbname. BillTaxDetails_SC_07_01. PO = dbname. BCollRateFile. PO)



I am writing this query in sp-

exec('SELECT * FROM dbname.' + @tablename + ',dbname.BCollRateFile WHERE (dbname.' + @tablename + '.id like " %' + @id + '% ") and (dbname.' + @tablename + '.TaxRollYr = dbname.BCollRateFile.TaxRollYr) and (dbname.' + @tablename + '.PO = dbname.BCollRateFile.PO)')

my problem is near " '.id like " %' + @id + '% ")". Can you please correct query? Problem is because of single quote as I need to close the command in exec(‘ ‘) in single quote as well as need to put single quote around id as it is string.
any help would be appreciated.
Jun 19 '08 #1
5 2577
ck9663
2,878 Expert 2GB
Use two quotes...

Expand|Select|Wrap|Line Numbers
  1. exec(''SELECT * FROM dbname.' + @tablename + ',dbname.BCollRateFile WHERE (dbname.' + @tablename + '.id like ' %' + @id + '% ') and (dbname.' + @tablename + '.TaxRollYr = dbname.BCollRateFile.TaxRollYr) and (dbname.' + @tablename + '.PO = dbname.BCollRateFile.PO)'')
-- CK
Jun 19 '08 #2
Use two quotes...

Expand|Select|Wrap|Line Numbers
  1. exec(''SELECT * FROM dbname.' + @tablename + ',dbname.BCollRateFile WHERE (dbname.' + @tablename + '.id like ' %' + @id + '% ') and (dbname.' + @tablename + '.TaxRollYr = dbname.BCollRateFile.TaxRollYr) and (dbname.' + @tablename + '.PO = dbname.BCollRateFile.PO)'')
-- CK
throwing error-
Msg 156, Level 15, State 1, Procedure usp_ts, Line 24
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Procedure usp_ts, Line 24
Incorrect syntax near ' + @tablename + '.
Jun 19 '08 #3
ck9663
2,878 Expert 2GB
My bad...

try:

Expand|Select|Wrap|Line Numbers
  1. exec
  2. ('SELECT * FROM dbname.' + @tablename + ',dbname.BCollRateFile WHERE (dbname.' + @tablename + '.id like ''%' + @id + '%'') and (dbname.' + @tablename + '.TaxRollYr = dbname.BCollRateFile.TaxRollYr) and (dbname.' + @tablename + '.PO = dbname.BCollRateFile.PO)')
-- CK
Jun 20 '08 #4
dbpros
15
You can usually get around these problems by adding more ' (single quotes) until the parser is happy.

http://www.db-pros.com
Jun 23 '08 #5
Whenever I have to quote a query (whether it is with OPENQUERY or EXEC), I simply type it regularly without the surrounding extra single quotes and then do a replace all on the query (replace ' to '')
Jun 24 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rasjid Wilcox | last post by:
Hi, I found references on Google to a discussion a little while ago about using RestrictedPython instead of rexec and Bastion. But I've had trouble finding documentation. Below is my test...
7
by: Alex Vorobiev | last post by:
hi there, i am using sql server 7. below is the stored procedure that is giving me grief. its purpose it two-fold, depending on how it is called: either to return a pageset (based on page...
10
by: Julian Smith | last post by:
I've been playing with a function that creates an anonymous function by compiling a string parameter, and it seems to work pretty well: def fn( text): exec 'def foo' + text.strip() return foo ...
1
by: Ryan | last post by:
Hello, I was trying to user the code below to run a DTS job. The job fails when I get to the piece of code that begins: EXEC @rc = sp_OAMethod @PackageToken, 'LoadFromSQLServer', ... It...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
1
by: dvroman | last post by:
The procedure works without problems for the basic 'textbody' and 'htmlbody' type messages. The problem is I would like to get the CreateMHTMLBody method working. This works beautifully as a VBScript...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
1
by: Matimus | last post by:
I think I'm going to create a new issue in Pythons issue database, but I wanted to run it by the news group first. See if I can get any useful feed back. The following session demonstrates the...
0
by: Chris Rebert | last post by:
On Fri, Nov 14, 2008 at 10:40 AM, Indian <write2abdul@gmail.comwrote: You don't need and shouldn't be using `exec` here. It appears as though you're just doing the following in a much more obtuse...
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
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
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
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...

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.