Source :HIVE Table having three columns, Column1,Column2,Column3
Destination : SQL server
I wish to call a stored procedure in sql server using Apache Export Command to insert data of HIVE to SQL Server. The stored procedure accepts 1 argument . The Stored Procedure look like this as shown below
CREATE PROCEDURE dbo.Insert #Column1 nvarchar(30)
AS
SELECT *
FROM dbo.SampleTable
WHERE ColumnName= #Column1
GO
Can any body tell me the syntax for the same?
Related
I have an Oracle server that has a package with a procedure like so:
CREATE OR REPLACE PACKAGE TEST AS
PROCEDURE name (id IN NUMBER, name OUT VARCHAR2);
END TEST
CREATE OR REPLACE PACKAGE BODY TEST AS
PROCEDURE name (id IN NUMBER, name OUT VARCHAR2) IS
BEGIN
SELECT NAME as name
INTO name
FROM NAMES
WHERE ID = id;
END name;
END TEST;
I cannot modify the Oracle server in any way.
And I have a SQL Server connected to the Oracle server via linked server (provider is 'Oracle Provider for OLE DB' if that's important).
I'm trying to get the results of the procedure 'name' inside SQL Server like so:
DECLARE #id int = 117, #name nvarchar(200)
EXEC ('begin TEST.name(?,?); END;', #id, #name OUTPUT) AT OrclDB
SELECT #name
Some names contain Lithuanian letters (i.e. ą č ę ė į š ų ū ž) and for some reason letters š and ž are returned properly, but letters ą č ę ė į ų ū are instead changed to: a c e e i u u.
If I select the name directly from the table using OpenQuery I get the correct result
SELECT *
FROM OpenQuery(OrclDB, 'SELECT NAME from NAMES WHERE ID = 117')
Is there a way to get the correct result from the package procedure?
The package procedure return the names correctly if ran directly in the oracle server.
The problem only occurs when trying to access the procedure from SQL Server.
I have a SQL Server stored proc which takes #RegionIds as a input parameter which is of type BigIntList.
It looks like below.
CREATE PROC #RegionTestProc
#RegionIds AS dbo.BigIntList
AS
BEGIN
/* SQL Query */
END
Now i have the #RegionIds values with me, lets say those values are [12,34,456,32,23,57], I would like to execute this stored procedure from the Sql Server Management studio query editor window and see the ouput
EXEC #RegionTestProc #RegionIds = '12,34,456,32,23,57'
How do i pass the RegionIds to the stored proc ? the above query is throwing below error.
Operand type clash: varchar is incompatible with BigIntList
Assuming BigIntList is just a user defined table type with a column called id of type BIGINT (as the name implies):
DECLARE #RegiondIds BigIntList;
INSERT #RegionIds (id)
VALUES (12),(34),(456),(32),(23),(57);
EXEC #RegionTestProc #RegionIds = #RegionIds;
I am using SSIS 2016. I need to execute a stored procedure that returns 4 result sets. I only need to keep the first result set and write this to a table. I can not modify the stored procedure. I do not care about any of the data returned in the other result sets. The stored procedure is in a SQL Server 2016 database. Results will also reside in SQL Server 2016.
I currently have this process running in SSIS 2008 using the "SQL Command" data access mode in an OLE DB Source like below. I have this in a For Each Loop Container to pass a series of param values to the stored procedure as I execute it multiple times for different param values on a daily basis.
SET FMTONLY OFF;
EXEC myProc
#Param1 = ?,
#Param2 =?,
#Param3 = ?;
By default SSIS 2008 is only returning the first result set, which has worked for me as I only care about the first result set.
I am using the Native OLEDB SQL Server client. From what I have read, it has changed the way it handles multiple result sets. I have used the WITH RESULT SETS to define the first result set but if I execute SSIS will fail indicating other result sets need to be defined.
In short, what is the best approach to duplicate what works in SSIS 2008 in SSIS 2016?
Solution Overview
I made 2 Experiments on that issue, the first experiments showed that in case of stored procedures with no parameters, nothing changed in SQL Server 2016 and SSIS 2016, the first Result Set is returned and others are ignored.
The second experiment showed that when using parameters, this will throw an exception, so you have to define metadata using WITH RESULT SETS option, then remove this option.
Detailed Solution
Experiment 1
The following experiment are made using SQL Server 2016 and Visual Studio 2015 with SSDT 2016
First i created this stored procedure
CREATE PROCEDURE sp_Test
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
GO
Then i added a Data flow task to SSIS package
Added an OLEDB Source, Recordset destination
In OLEDB source i select the Data access mode to SQL command
an use the following commnad
EXEC sp_Test
When clicking on Columns Tab it shows the first ResultSet structure
And we i executed the package it runs succesfully
Experiment 2
I changed the stored procedures to the following:
ALTER PROCEDURE [dbo].[sp_Test]
#param1 varchar(10),
#param2 varchar(10),
#param3 varchar(10)
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP 10 PersonType,NameStyle,Title ,#param2 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person]
SELECT TOP 10 PersonType,Firstname,Lastname,#param3 as 'Param'
FROM [AdventureWorks2016CTP3].[Person].[Person_json]
END
And i used the following SQL Command in the OLEDB Source:
EXEC sp_Test ?,?,?
WITH RESULT SETS (
(
PersonType NVarchar(10),
NameStyle NVarchar(10),
Title NVarchar(10),
Param Varchar(10)
)
)
And i mapped the parameters correctly.
When running the package it throws the following exception.
[OLE DB Source 2] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E14.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 11.0" Hresult: 0x80040E14 Description: "EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), and the statement tried to send more result sets than this.".
After that i tried to remove the With RESULT SETS option, so the command is :
EXEC sp_Test ?,?,?
I tried to execute the package again, so it is executed with no errors.
Conclusion
Try to use the WITH RESULT SETs option to define the OLEDB Source metadata, after that the metadata is defined, just remove this option and run the package, so it will just take the first Result Set succesfully.
I know it's not exactly what you are asking for, but maybe you could create another stored proc that will return only the first result set for you? (Without touching the first stored proc.) Or insert the data into a table for you and then you just read the data?
This is running on SQL Server 2008, but the database is in SQL Server 2000 compatibility mode (this I cannot change).
A temporary table is created at the beginning of the stored procedure and then insert into via an EXEC statement from dynamically generated SQL. How is this executing successfully, even though the temporary table (should be, from my understand) is out the execution scope, or does this get bypassed temporarily when performing these statements inside a stored procedure?
Example below:
CREATE PROCEDURE myProc (Param1 int....)
AS
BEGIN
Declare #SQL varchar(max) = null
Create table #tempTable
(
ID int,
Code1 varchar(255),
...
)
SET #SQL = 'insert into #tempTable '
+ ...
EXEC(#SQL)
Select * from #tempTable
END
Any help understanding this would be greatly appreciated!
Thanks!
Shortly:
The temporary table created in the outer batch is accessible inside the dynamic SQL but not vice versa.
See doc
A local temporary table created in a stored procedure is dropped
automatically when the stored procedure is finished. The table can be
referenced by any nested stored procedures executed by the stored
procedure that created the table. The table cannot be referenced by
the process that called the stored procedure that created the table.
Scenario: get trigger-generated primary key when calling INSERT INTO from SQL Server linked server to Oracle
Given
Oracle 11g table with columns PRIMARY_KEY_ID, FIELD1, FIELD2, CREATE_DATE. Table has "BEFORE INSERT" trigger that selects NEXTVAL from a sequence into PRIMARY_KEY_ID field.
SQL Server 2008 R2 with Linked Server to the Oracle database containing the table above.
When I insert a record into the Oracle table, then I want to retrieve the trigger-generated primary key.
How do I do this?
Make sure these properties are set on the SQL Server linked server:
RPC=True
RPC Out=True
Execute this code in SQL Server:
DECLARE #Field1 NVARCHAR(42);
DECLARE #Field2 NVARCHAR(42);
DECLARE #PrimaryKeyValue INT;
EXECUTE (
'begin INSERT INTO MYSCHEMA.MYTABLE (
FIELD1
,FIELD2
,CREATE_DATE
)
VALUES (
?
,?
,sysdate
) RETURNING PRIMARY_KEY_ID INTO :PrimaryKeyValue; end;'
,#Field1
,#Field2
,#PrimaryKeyValue OUTPUT
) at oracle_linked_server;
Notes
begin and end; are required in the statement.
The #PrimaryKeyValue variable declared in SQL Server is the same as the :PrimaryKeyValue output parameter; Oracle uses a colon prefix for parameters.
See Calling Oracle stored procedure with output parameter from SQL Server, which provided the inspiration for this answer.