Migration from SQL Server to snowflake - snowflake-cloud-data-platform

I need to convert the SQL Server stored procedure to snowflake - can you please help us?
CREATE PROCEDURE dbo.analyticsGetFilterNames
#product NVARCHAR(20) = '',
#franchise NVARCHAR(20) = ''
AS
BEGIN
SET NOCOUNT ON;
IF #product = 'Menus' AND #franchise = 'Nissan'
BEGIN
SELECT * FROM table1
END
ELSE -- Triage & other brands
BEGIN
SELECT * FROM table2
END
END

you need to make a Tabular SQL UDF(TSUDF) in snowflake :
CREATE OR REPLACE FUNCTION analyticsGetFilterNames (
product NVARCHAR(20)
, franchise NVARCHAR(20)
)
RETURNS TABLE (<output_col_names>) AS
$$
IF product = 'Menus' and franchise = 'Nissan'
BEGIN
SELECT * from table1
END
ELSE -- Triage & other brands
BEGIN
SELECT * from table2
END
$$

Currently you can only write JS in Snowflake's Stored Procedure, the SQL scripting feature is not yet publicly available.
Please refer to the documentation on how to write JS SP in Snowflake:
https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html

Related

After creating a stored procedure and querying the table created,returns 'Invalid object name' even after successful creation of the stored procedure

I had written and successfully created a stored procedure by the query
CREATE PROCEDURE [dbo].[Table_Load]
AS
BEGIN
SET NOCOUNT ON
DECLARE #Row_Count_Inserted BIGINT
DROP TABLE IF EXISTS DBB.dbo.Table;
SELECT *
INTO DBB.dbo.Table
FROM
(SELECT *
FROM DBB.dbo.customer_table) y
SET #Row_Count_Inserted = ##RowCount
SELECT #Row_Count_Inserted Row_Count_Inserted
END
This shows that the stored procedure is created and is present in the database. But when I query the table 'Table' using
SELECT * FROM DBB.dbo.Table
I get an error
Invalid object name
How can I solve this issue? I have refreshed the database as well but it does not work.
Here is your procedure greatly simplified to remove a lot of extra code. Assuming you have the table customer_table this will work just fine.
CREATE or alter PROCEDURE [dbo].[Table_Load] As
Begin
SET NOCOUNT ON
drop table If Exists dbo.MyTable;
Select *
into MyTable
from customer_table
select Row_Count_Inserted = ##RowCount
End
GO
exec Table_Load
GO
select * from MyTable

Insert first table into temp table when procedure returns multiple tables in SQL Server 2014

I have 2 procedures proc_Data, proc_FetchData.
I'm calling proc_Data from within proc_FetchData. proc_Data returns 2 tables. I want to insert only the first table into a temp table in my second procedure and use it further.
The problem is I cannot change proc_Data in any way as this is a very old procedure used in various parts of our application.
Sample code for reference
create procedure proc_Data
As
Begin
select 'Apples'
select 'Oranges','Grapes'
end
create procedure proc_FetchData
As
Begin
create table #temp(Data varchar(30))
insert into #temp
exec Test_proc
select * from #temp
end
I'm using SQL Server 2014 - is there any way to achieve this? Thanks in advance.
You cant do it that way, you can split your first SP into 2 and call it in your second SP, like,
create procedure proc_Data1
As
Begin
select 'Apples'
end
GO
create procedure proc_Data2
As
Begin
select 'Oranges','Grapes'
end
GO
ALTER procedure proc_FetchData
As
Begin
create table #temp(
Data varchar(30)
)
insert into #temp
exec proc_Data1
select * from #temp
end
To insert only the first table you can use OPENROWSET.
create procedure proc_FetchData
As
Begin
declare #userData TABLE(Data NVARCHAR(30))
INSERT INTO #userData
SELECT * FROMOPENROWSET('SQLNCLI','Server=[server];Trusted_Connection=yes;','EXEC [database name].dbo.proc_Data')
select * from #userData
end
If you need to access all resultsets you will need to use SQLCLR proc: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/da5328a7-5dab-44b3-b2b1-4a8d6d7798b2/insert-into-table-one-or-multiple-result-sets-from-stored-procedure?forum=transactsql
I suggest you add a new input parameter with a default value to your procedure and return different datasets depended on its value.
ALTER procedure proc_Data
#Mode INT = 0
As
Begin
IF #Mode = 0 BEGIN
select 'Apples'
select 'Oranges','Grapes'
END
ELSE BEGIN
select 'Apples'
END
end
go
ALTER procedure proc_FetchData
As
Begin
create table #temp(Data varchar(30))
insert into #temp
exec proc_Data #Mode = 1
select * from #temp
end
After doing so, you will be able to use your proc_Data in any old queries without changes, whereas its output will be different with different #Mode values if you need.

Displaying All Tables Created By a User PL SQL Procedure

I am trying to create a procedure that displays all of the tables created by a specified user. Here is what I have so far:
CREATE OR REPLACE PROCEDURE user_tables
#username VARCHAR(50),
#result varchar(100000) OUTPUT
AS
BEGIN
-- Execution section
Set result = SELECT * FROM ALL_TABLES WHERE OWNER = #username;
EXCEPTION
-- Exception section
WHEN no_data_found THEN
dbms_output.put_line('User does not exist');
WHEN others THEN
dbms_output.put_line('Error!');
END;
I was wondering if this is the right path, and how I should go about completing this task
Here is a hint
BEGIN
FOR tn IN (SELECT table_name FROM all_tables /*WHERE owner = vc_User_Name*/)
LOOP
-- Display some stuff
DBMS_OUTPUT.PUT_LINE(tn.table_name);
END LOOP;
END;
/

Recreating db Snapshots on scheduled basis

I am new to db world. I have to create a job that will drop the db snapshots and recreate it on daily basis. But I was wondering how can I validate once the snapshots is setup.
You can use SQLAgent to accomplish this: Creation & Deletion of Database Snapshot by SQL Agent Job
--Step 1 :-
DECLARE #CREATE_SS VARCHAR(MAX)
DECLARE #DT VARCHAR(100)
SET #DT = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,GETDATE(),120),’-‘,’_’),’ ‘,’_’),':’,’_’)
SET #CREATE_SS =
‘CREATE DATABASE TEST_’+#DT+’ ON
( NAME = TEST, FILENAME =
”C:\TEST_’+#DT+’.SS” )
AS SNAPSHOT OF TEST’
EXEC (#CREATE_SS)
--Step 2 :-
IF(SELECT COUNT(*) FROM SYS.DATABASES WHERE SOURCE_DATABASE_ID = DB_ID(‘TEST’))>1
BEGIN
DECLARE #DROP_SS VARCHAR(MAX)
DECLARE #SS_NAME VARCHAR(100)
SELECT TOP 1 #SS_NAME = NAME FROM SYS.DATABASES WHERE SOURCE_DATABASE_ID = DB_ID(‘TEST’)
ORDER BY CREATE_DATE ASC
SET #DROP_SS = ‘DROP DATABASE ‘+ #SS_NAME
EXEC (#DROP_SS)
END
For the validation part,you can use below query..
if exists(
SELECT * FROM sys.databases WHERE name = 'somename'
AND source_database_id IS NOT NULL)
begin
print 'database snapshot created'
end

unable to call a user defined function in sql query of sql server 2005

Im unable to call a user defined function in an sql query of sql server.
my qyery is
SELECT Empnum(delimeter_test) as del FROM FNSPLIT_TEST WHERE Len(delimeter_test) >1
and my function is
ALTER FUNCTION [dbo].[Empnum] (#RAWDATA NVARCHAR(300))
RETURNS VARCHAR(30)
AS
BEGIN
DECLARE #TEMP1 NVARCHAR(300)
--DECLARE #TEMP2
IF(CHARINDEX('Disabled',#RAWDATA,1)) <> 0
BEGIN
DECLARE SPLITTEXT CURSOR FOR
SELECT [item] FROM fnSplit(#RAWDATA,',')
-- SET #TEMP1=dbo.fnSplit(#RAWDATA,',')
OPEN SPLITTEXT
FETCH SPLITTEXT INTO #TEMP1
CLOSE SPLITTEXT
DEALLOCATE SPLITTEXT
-- INSERT INTO #EMPNUM1 SELECT #TEMP1
-- SET #TEMP1=dbo.fnSplit(#RAWDATA,',')
END
RETURN #TEMP1
END
For some reason UDF's require the schema name:
SELECT dbo.Empnum(delimeter_test) as del FROM FNSPLIT_TEST WHERE Len(delimeter_test) > 1
Also, here is a method of concatenating text strings without a cursor:
Concatenate many rows into a single text string?

Resources