Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
| Search | Today's Posts | Mark Forums Read
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 1st, 2010, 10:47 AM
Authorized User
 
Join Date: Jun 2004
Location: Aurangabad, Maharashtra, India.
Posts: 54
Thanks: 0
Thanked 1 Time in 1 Post
Default How to insert values from Variable and table in single query

Hello All,

I am stuck with a problem. Could somebody show me as to how to accomplish following thing in Oracle 10g?

I need to insert 4 different claim number from a table along with a constant value from variable.

for example:

'some value', 123456
'some value', 223344
'some value', 555555
'some value', 123457

Possible Solution:
=================

DECLARE
XCONST VARCHAR2(20);
BEGIN
XCONST:= 'some value';
INSERT INTO A_TABLE(SOME_VALUE, CLAIMNUMBER)
SELECT (SELECT XTEST FROM DUAL), CLAIMNUMBER FROM TBLVOUCHERS WHERE ROWNUM <=4;
END;

Also I would want to avoid using any loop or cursor here. Is it possible?

Thank in Advance
Vinod
__________________
Vinod Pawar
United States
  #2 (permalink)  
Old July 2nd, 2010, 12:40 AM
Authorized User
 
Join Date: Jun 2004
Location: Aurangabad, Maharashtra, India.
Posts: 54
Thanks: 0
Thanked 1 Time in 1 Post
Cool SOLVED!!! Here is the solution with bind variable

DECLARE
XCONST VARCHAR2(20);
BEGIN
XCONST := 'some value';
EXECUTE IMMEDIATE 'INSERT INTO A_TABLE(SOME_VALUE, CLAIMNUMBER) SELECT (SELECT :b1 FROM DUAL), CLAIMNUMBER FROM TBLVOUCHERS WHERE ROWNUM <=4'
USING XCONST;
END;

In the code :b1 is a bind variable and "USING" replaces bind variable value with the passed value (passed variable value).

Thanks
__________________
Vinod Pawar
United States
  #3 (permalink)  
Old July 2nd, 2010, 02:54 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I haven't programmed with Oracle for ages but I can't believe you need to use dynamic SQL for this simple task. With SQL Server the code would resemble (allowing for Oracle's syntax):
Code:
SELECT :XConst, CLAIMNUMBER FROM TBLVOUCHERS WHERE ROWNUM <=4;
__________________
Joe
http://joe.fawcett.name/
  #4 (permalink)  
Old July 2nd, 2010, 12:12 PM
Authorized User
 
Join Date: Jun 2004
Location: Aurangabad, Maharashtra, India.
Posts: 54
Thanks: 0
Thanked 1 Time in 1 Post
Default

ugh,

Not sure, I guess I was doing something wrong when I tried to select variable along with other fields of a table and it showed errors all the time.

BTW, thank you for your response, this saved me a lot of painful time.

You Rock!

Thanks
Vinod
__________________
Vinod Pawar
United States


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reteive values from the table and store them in a variable bex ASP.NET 2.0 Basics 2 February 27th, 2009 06:03 AM
Check two string values on a single variable?? ramesh.kumarm XSLT 2 June 29th, 2006 03:29 AM
insert multiple records into a table from values Deepak Chauhan Oracle 3 May 12th, 2006 10:35 PM
Insert ODBC Values into Table Ben Access VBA 3 January 26th, 2005 01:12 PM
insert value of variable into an existing table mountaindew7612 SQL Server 2000 5 October 16th, 2004 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.