error while creating table in procedure [closed] - sql

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i want to create a procedure in which i can create a table.
so i have done this
CREATE OR REPLACE PROCEDURE test_proc
BEGIN
execute immediate 'CREATE TABLE ABC_TABLE AS SELECT * FROM XYZ_TABLE WHERE 1=0';
END;
but after compiling i get following error.
Encountered the symbol "BEGIN" when expecting one of the following: ( ; is with authid as cluster compress order using compiled wrapped external deterministic parallel_enable pipelined The symbol "is" was substituted for "BEGIN" to continue.
can any one tell me reason for this and how to create the table in procedure.
thanks in advance...

You are missing "AS" between test_proc and BEGIN.
So It should be
CREATE OR REPLACE PROCEDURE test_proc AS
BEGIN

Related

Why is this Stored Procedure Inserting NULL values into the table? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to load data (2 columns) from XML_HOURS_LOAD (columns: code,product) to a table called STAGING (columns: code, product) and am getting null values inserted for both columns:
So I have the following stored procedure:
Create or Replace Procedure Cascade_Load (
p_code in XML_HOURS_LOAD.p_code%TYPE,
p_product in XML_HOURS_LOAD.p_product%TYPE
)
AS
BEGIN
INSERT INTO STAGING(code, product)
VALUES(p_code, p_product)
COMMIT;
END;
What am I doing wrong? Thanks in advance.
To answer your question of why it's inserting nulls, that is because you aren't providing any values to the procedure parameters when you execute it.
Based on what you stated in the question and your comment above, it seems you are missing some fundamental skills in working with Oracle. The code you wrote is a procedure, not a function, so you can't call it in a SELECT statement. A procedure is called inside of a plsql block. Your procedure as written takes two arguments, which you must pass to the procedure call via the calling code. The procedure code you wrote does not look for data from the XML_HOURS_LOAD table.
We've all been the new person learning Oracle. You'll want to look at some tutorials to get you started on the fundamentals of pl/sql coding to help clear up the differences between functions and stored procedures and how to use parameter arguments.
From what you wrote in your question, I believe this is the code you want:
DECLARE
p_code IS XML_HOURS_LOAD.code%TYPE,
p_product IS XML_HOURS_LOAD.product%TYPE;
CURSOR cXmlHoursLoadCursor IS (SELECT code, product FROM xml_hours_load); --You can add a WHERE condition to this cursor query
BEGIN
FOR v IN cXmlHoursLoadCursor LOOP
Cascade_Load(v.code, v.product);
COMMIT; --I recommend calling commit here instead of inside your stored procedure so that the calling code has control of the transaction state
END LOOP;
END;

How to handle compile time exception in pl/sql block?

Actually its a silly quiestion to ask because why would you allow compile time exceptions in your code, But my situation is some what different.
Actually I am writing a pl/sql block in which I am fetching table names at run time and then using that table name in a query in which I have a where clause " where maker ='AUTO_MAST_MAK' ".. Now the problem is that in some table that "maker" column is not available so the block is not getting compiled.
Can any body help me in solving my problem.. or any suggestions " should I change my approach to my problem"
Dynamic PL/SQL can handle compilation errors:
declare
compile_error exception;
pragma exception_init(compile_error, -06550);
begin
execute immediate q'<
begin
does not compile
end;
>';
exception when compile_error then
dbms_output.put_line('PL/SQL Block did not compile.');
end;
/
Why don't you check this column before generating of PLSQL block?
select table_name,column_name,data_type from user_tab_columns
Just check if the table has this column in runtime.

Can't execute a procedure [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
this is the procedure name :
procedure misowner.proc_kr_text_niki(valdate in varchar2 default '20040101'
, v_table varchar2 default 'KR_TEMP')
and i what to EXEC it. What kind of variables i have to put?
Given that you have default values for the parameters you should be able to surround it with a block like this, and execute it as a script if you are using a GUI tool like SQL developer or Toad
DECLARE
valdate VARCHAR2(10);
v_table VARCHAR2(30);
BEGIN
misowner.proc_kr_text_niki;
END;
This works for testing, then, as suggested by BazzPsychoNut, initialize the variables, pass them in and test some more.
You call it by providing two parameters. The first parameter (= variable) "valdate" is in varchar2. It is apparently a date inputted as text in the form YYYYMMDD. The second parameter "v_table" is a varchar2, probably the name of the table to query on.

Check if table exists, before performing operations on it? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
In searching for an answer to this question, I found this popular post on StackOverflow. Unfortunately, it doesn't work completely. The question is this:
Is there a way to check for existence of a table (or another object) before performing modifications (e.g. INSERT)? The before mentioned post suggests this:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'questionableTable'))
BEGIN
INSERT INTO dbo.questionableTable VALUES ('success!');
END
Error: Invalid object name 'dbo.questionableTable'.
The problem with this is that SQL Server fails when it parses the INSERT statement, stating that dbo.questionableTable doesn't exist. The previous INFORMATION_SCHEMA check doesn't seem to affect it.
Is there a way to write this kind of query? For SQL Server, in particular. But I would also like to see similar operations for other database systems, if such things exist.
The motivation behind this question is because we have multiple databases which contain subsets of each others' tables. What I would like is to have a single script that can be applied to all databases, and which only modified the tables that exist there (and doesn't error upon execution).
Use dynamic SQL via the EXEC() function:
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'questionableTable'))
BEGIN
EXEC('INSERT INTO dbo.questionableTable VALUES (''success!'')');
END
The EXEC() function executes a string as SQL, but being a string it isn't evaluated until executed, so the tables mentioned in the string don't need to exist at compile time. This allows the stored proc to be defined prior to the table being created.
I tested this on my local server and it seems to work:
if exists (select * from dbname.sys.tables where name='tablename')
begin
select * from dbname.dbo.tablename
end

What could cause SSMS to not parse SQL properly? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the darndest thing I've ever seen.
I created a proc from a template that uses SYSNAME as the parameter types. All portions of the proc that took the name from the parameter are throwing errors. Here is a sample:
IF EXISTS(select 1 from sysobjects where name=N'dbo.ms_lst_partner_break_types' and xtype='p')
BEGIN
PRINT 'DROP PROCEDURE dbo.ms_lst_partner_break_types'
DROP PROCEDURE dbo.ms_lst_partner_break_types
END
Here is the error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '_partner_break_types'.
The weirdest thing is that when I double click on dbo.ms_lst_partner_break_types SSMS highlights either ms_lst or _partner_break_types depending on where I click. Copy the script into Textpad and back, same problem. Remove _partner_break_types and suddenly it works.
Does anyone have any idea what gives?
I don't know why it happened, but Unicode character 0x1f was inserted into the script for some reason. It might be a bug in SSMS, but I don't think it's going to be answered that easily.
In SQL Server 2008, it's sys.objects. Also, the field to look for the "name" is different, as well as other general syntax:
IF EXISTS (SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[my_proc]')
AND type in (N'P', N'PC'))
The easiest thing to do is to right-click on the SP and select "script as drop to new query window" via the context menu heirarchy.