Does comment syntax exist for Oracle store procedures? - database

As table, I have
CREATE TABLE foo_tbl
(
COLUMN1 VARCHAR2(20)
);
COMMENT ON TABLE foo_tbl 'For look up.';
Is exist comment syntax for Oracle store procedure?
CREATE OR REPLACE PROCEDURE foo_user.bar_store_prod IS
BEGIN
dbms_output.put_line('Welcome ' || 'foo');
END;
/

No.
As per the documentation (21c, the latest version), a comment may only be added to the following entities:
table
view (materialized or not)
column
audit policy
edition
indextype
mining model
operator

Related

There is already an object named 'name' in the database - stored procedure error - SQL Server

I have two tables called timeus and usdpay in my SQL Server database. These two tables get updated every week.
I did small transformation and combined these two tables and created a new table called fluslabor.
I created fluslabor using the stored procedure shown here, and it is working:
CREATE PROCEDURE [dbo].[sp_uslabor]
AS
BEGIN
SET NOCOUNT ON
IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL
TRUNCATE TABLE [dbo].[fluslabor];
SELECT ut.employee_code
,ut.employee_name
,ut.department_desc
,ut.location_desc
,ut.hour
,ut.projects_code
,ut.in_effective_time
,ut.out_effective_time
,ut.date
,ut.id
,p.rate
,(p.rate * ut.hour ) as Labour_Cost
INTO fluslabor
FROM timeus ut
LEFT JOIN usdpay p ON (TRIM(ut.id) = TRIM(p.id) AND ut.date = p.date)
WHERE ut.projects_code NOT LIKE '0%'
END
Today I got new data updated in my two tables timeus and usdpay.
When I execute my stored procedure, SQL Server is throwing this error:
Msg 2714, Level 16, State 6, Procedure SP_uslabor, Line 12 [Batch Start Line 38]
There is already an object named 'fluslabor' in the database.
I need to truncate my table every time and load the new data. I checked the similar post, they said to use drop table option. I don't want to drop the table, just want to truncate and execute the procedure
Can anyone advise what is the issue here please?
The problem here is that the table fluslabor already exists in the database. what you are trying above the insert is checking the object existence and then truncating the same
There are two possible approaches that you can try here.
Instead if the TRUNCATE do a DROP TABLE. But This will also remove the existing user permissions on the table if you have provided specific custom access to the table to any of the users
IF OBJECT_ID(N'dbo.fluslabor', N'U') IS NOT NULL DROP TABLE [dbo].[fluslabor];
The safest approach will be change the SELECT .. INTO statement and convert it into INSERT INTO like this
INSERT INTO fluslabor ( <List your Destination columns> ) SELECT <List your Source columns> FROM <Source Query>
the 2nd approach will have the records loaded along with keeping all the existing permissions
IF EXISTS (SELECT 1 FROM sys.objects WHERE type = 'P' AND name = 'your SP name')
BEGIN
DROP PROCEDURE "your SP name";
END
GO
CREATE PROCEDURE "your SP name"
AS
BEGIN
.
.
.
.
try this one I guess this will help you.

Is there any equivalent in Snowflake for OBJECT_ID() in SQL Server?

The OBJECT_ID function Returns the database object identification number of a schema-scoped object in SQL SERVER.
Could anyone suggest an equivalent function in Snowflake that can be used inside a stored procedure?
I need to migrate the below code to Snowflake:
CREATE PROCEDURE test_procedure
(#Var1 INT )
AS
BEGIN
IF #Var1 = 1
BEGIN
IF OBJECT_ID('db1.Table1') IS NOT NULL
DROP TABLE Table1;
END;
END;
The pattern:
IF OBJECT_ID('db1.Table1') IS NOT NULL
DROP TABLE Table1;
is the old way to check if table exists before trying to drop it.
Currently both SQL Server and Snowflake supports IF EXISTS clause:
DROP TABLE IF EXISTS <table_name>;
db<>fiddle demo
The closest I can think of will be to use a function like:
CREATE OR REPLACE FUNCTION OBJECT_ID(NAME VARCHAR) RETURNS STRING
LANGUAGE SQL
AS
$$
SELECT OBJECT_SCHEMA || '.' || OBJECT_NAME FROM INFORMATION_SCHEMA.OBJECT_PRIVILEGES WHERE
(OBJECT_SCHEMA || '.' || OBJECT_NAME) = NAME
$$;
The snowflake object_privileges view is the closest information_schema view listing all db elements.
However as noted on previous answers, the IF EXISTS on create and drop statements, makes the usage of this function unnecessary

Migrate trigger Oracle to SQL Server

People,
I need migrate a Oracle trigger to SQL server, but I could not do.
The trigger in Oracle is very simple:
CREATE OR REPLACE TRIGGER trigger_teste
BEFORE INSERT OR UPDATE
ON teste
FOR EACH ROW
DECLARE
BEGIN
:new.id := (coalesce(:NEW.id, 0));
:new.vlr_sal := (coalesce(:NEW.vlr_sal, 0.00));
END;
I tried several ways but none successfully!
Thank for help!
My T-SQL is a bit rusty, but something like this should work. Note that SQL server does not have row level triggers, only statement level triggers.
CREATE TRIGGER trigger_teste
ON teste
BEFORE INSERT OR UPDATE
AS
update inserted
set id = coalesce(id, 0),
vlr_sal = coalesce(vlr_sal, 0.0)
GO
(Not sure if I got missed a semicolon or not. I never understood when SQL Server needs or deosn't need one)
See the manual for more details:
http://msdn.microsoft.com/en-US/library/ms189799%28v=sql.90%29
http://msdn.microsoft.com/en-US/library/ms191300%28v=sql.90%29
This is not an appropriate use of triggers in any flavour of RDBMS. The SQL standard allows us to define default values when we create the table using the DEFAULT constraint syntax. Both Oracle and SQL Server have this.
Obviously you haven't do this when you created the table. The good news is we can use ALTER TABLE to add default constraints. Something like this
alter table teste
alter column id set default 0
That's for SQL Server. In Oracle it would be:
alter table teste
modify id default 0
As the nameless equine points out, a complete replacement for the trigger must include NOT NULL constraints on the affected columns. If the existing table lacks not null constraints we can add them using the same syntax as shown above, replacing the DEFAULT clause with NOT NULL - or even combining the two clauses in the same statement.

SQL Server parallels to Oracle Alter Table Set Column Unused?

Is there a parallel in Microsoft SQL Server (2005, preferably) for the Oracle functionality of setting a column to be unused? For example:
ALTER TABLE Person SET UNUSED Nickname;
Nothing turns up in searches, so my thought is this feature must be Oracle specific.
Don't think there's anything like that in SQL server.
You could create a 1:1 relation to a new table containing the hidden columns:
insert into NewTable
select (keycol, Nickname) from ExistingTable
alter table ExistingTable drop column Nickname
That way you still have the data, but the column is in a table nobody knows about.
Alternatively, you could use column level permissions:
DENY SELECT (Nickname) ON ExistingTable TO domain\user
DENY SELECT (Nickname) ON ExistingTable TO public
...
This will return an error when someone tries to read the column. The big disadvantage of this method is that select * will also fail.
There is no equivalent statement, but depending on your need you could probably write a trigger to roll back any changes if made.

Migrating from SQL Server to ORACLE ¿what to do with temporary tables?

Currently we use SQL Server and we have A LOT (read around 5.000) different scripts that create on the fly temporary tables.
Now we are migrating to ORACLE, so we cannot create on the fly temporary tables.
Any ideas?
Thanks in advance
You'd probably want to dynamically create the tables with an execute immediate whenever you need a temporary table:
-- creating the table
begin
execute immediate q'!
create table tmp_foo_bar (
col_1 number,
col_2 varchar2(50),
etc date
) !';
end;
/
-- using the table:
insert into tmp_foo_bar values (42, 'forty-two', sysdate);
-- dropping the table:
begin
execute immediate 'drop table tmp_foo_bar';
end;
/
Oh boy, that is a lot of temporary tables.
Have you had a look at Oracle's SQL Developer tool? It's free and it comes with a Migration Workbench which can help you with the journey.
With regards to temporary tables it appears that the OMWB will create temporary tables from the T-SQL statements. Find out more.
Caveat: I have never undertaken such a migration myself so I am not guaranteeing it. But with 5000 scripts to migrate it has to be worth your while to evaluate it.
What about Oracle Global Temporary Tables?
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
column1 NUMBER,
column2 NUMBER
) ON COMMIT DELETE ROWS; -- or use ON COMMIT PRESERVE ROWS to keep data until the end of your session.

Resources