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

Quotes in dynamic sql

A E
Hi,

I am trying to execute dynamic sql but I can't seem to get the qoutes right.

My code is

qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like %''|| trim(crmid) ||''%'';

Can anyone help?

Alex

Nov 12 '05 #1
2 5375
Shouldn't it be:

qry := 'SELECT * from '|| trim(realname) ||' where crmid like ''%'||
trim(crmid) ||'%''';

The contents of qry should be:

'SELECT * from blahblah where crmid like '%crmid%'

Your errors (I think) are here:
qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like %''||
^1 ^1 ^1 ^2

trim(crmid) ||''%'';
^1 ^3

^1 = only need a single quote
^2 = the quotes are after the % when they should be before
^3 = you have a missing closing single quote

Of course, if the above is in a pl/sql procedure, then you'll need to
double up all the quotes (I think).

Hope that helps.

John Sidney-Woollett

A E said:
Hi,

I am trying to execute dynamic sql but I can't seem to get the qoutes
right.

My code is

qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like %''||
trim(crmid) ||''%'';

Can anyone help?

Alex

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 12 '05 #2
A E
I tried doubling the quotes because I am running a pl/pgsql procedure, did not work. Full code context:

begin
select into allowed secverf(username, talias);
if allowed = 0 then
RAISE NOTICE ''User not authorized to perform retrieve.'', allowed;
RETURN null;
else
select into realname tablename from applicationaliases where aliasname like ''%''|| trim(talias) ||''%'';
if length(crmid) = 0 then
qry := ''SELECT * from ''|| trim(realname);
FOR objectdefinition IN EXECUTE qry LOOP
RETURN NEXT objectdefinition;
END LOOP;
else
qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like ''%''|| trim(crmid) ||''%'''';
FOR objectdefinition IN EXECUTE qry LOOP
RETURN NEXT objectdefinition;
END LOOP;
end if;
end if;
RETURN null;
end;'
LANGUAGE 'plpgsql' VOLATILE;

John Sidney-Woollett <jo****@wardbrook.com> wrote:
Shouldn't it be:

qry := 'SELECT * from '|| trim(realname) ||' where crmid like ''%'||
trim(crmid) ||'%''';

The contents of qry should be:

'SELECT * from blahblah where crmid like '%crmid%'

Your errors (I think) are here:
qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like %''||
^1 ^1 ^1 ^2

trim(crmid) ||''%'';
^1 ^3

^1 = only need a single quote
^2 = the quotes are after the % when they should be before
^3 = you have a missing closing single quote

Of course, if the above is in a pl/sql procedure, then you'll need to
double up all the quotes (I think).

Hope that helps.

John Sidney-Woollett

A E said:
Hi,

I am trying to execute dynamic sql but I can't seem to get the qoutes
right.

My code is

qry := ''SELECT * from ''|| trim(realname) ||'' where crmid like %''||
trim(crmid) ||''%'';

Can anyone help?

Alex

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)
Nov 12 '05 #3

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

Similar topics

6
by: Cyrus D. | last post by:
Hi guys, I haven't done that much research on this topic but it seems I can use either the single quotes or the double quotes. SInce I am so used to C(++) I prefer the double quotes and am...
11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
18
by: Colin Steadman | last post by:
My ASP page allows user to enter comments into a form. To avoid errors I'm having to strip out double quotes before saving to the database. Is there anyway to encode these so that I can store...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
4
by: (PeteCresswell) | last post by:
Is his just a flat-out "No-No" or is there some workaround when it comes time for SQL searches and DAO.FindFirsts against fields containing same? I can see maybe wrapping the value searched for...
4
by: Darrel | last post by:
I have a lot of fields where people can enter in data. If they enter in anything with an apostrophe, this messes up the SQL query and errors out. I know I shouldn't allow quotes and apostrophes...
11
by: Elmo | last post by:
Hi all! I am not very proud to ask this but here is my problem: string code = "\'13\'" The string code will have to contain following info: '51','52','63','other'... to get certain info...
4
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or...
5
by: SnehaAgrawal | last post by:
Hi I want to know how can we use single quotted string as a part of dynamic sql Consider the following code exec('select branchid from '+@tablename+' where Entrystatus='NEW' AND BranchName=...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.