Parameter variable schema oracle - database

I'm newbie using Oracle.
I have a question: in SQL Server, we can use:
DECLARE DBNAME1 VARCHAR(20)
DECLARE DBNAME2 VARCHAR(20)
SET #DBNAME1 ='TEST_DB'
SET #DBNAME2 ='TEST_DB2'
INSERT INTO #DBNAME1.TABLECORE
SELECT *
FROM #DBNAME2.TABLENONCORE T
What is the equivalent of this script in if #DBNAME == Schema in Oracle?

We can manage it but creating db link on dbname2 :
create public database link dbname2 connect to myschema using 'abc-scan.mycompany.com.tr:1521/dbname2.mycompany.com.tr';
and calling the following dml statement from dbname1 :
insert into tablecore select * from tablenocore#dbname2;

Related

Insert from SQL to SQLBase with linked Server

how can i insert from SQL Server to a SQLBase Database using linked Server?
Here are various examples of the correct syntax for SQLServer to SQLBase assuming your LinkedServer is called 'ISLANDLINK' . Note the two dots.
or Go here for the full script and explanation : SQLServer to SQLBase via LinkedServer
or Go here for another example: SQLServer to SQLBase via LinkedServer (more)
--Select:
SELECT * FROM OPENQUERY( ISLANDLINK, 'Select * from SYSADM.BUDGET where DEPT_ID = ''MIS'' ')
--Update:
UPDATE [ISLANDLINK]..[SYSADM].[BUDGET]
SET [BGT_YEAR] = 2016
WHERE DEPT_ID = 'MIS'
GO
--Insert:
INSERT INTO [ISLANDLINK]..[SYSADM].[EMPLOYEE]
([EMPLOYEE_ID]
,[LAST_NAME]) VALUES (99 ,'PLUMAS' )
GO
--Delete:
DELETE FROM [ISLANDLINK]..[SYSADM].[EMPLOYEE]
WHERE [LAST_NAME] = 'PLUMAS'

copy results of a view query into azure sql

I'm trying to create historic data for my company; previously i asked how to copy a table with a timestamp into the same mssql database. realistically, it dawned on me that what i really need to do is the following
1) log onto my local mssql box
2) run a query of a view (select * from view_whatever)
3) copy the results of the query and add a timestamp into another database, hosted using azure SQL
if i were doing this from the same database to another table, i'd something like this:
DECLARE #copyDate DATETIME2 = CURRENT_TIMESTAMP
INSERT INTO Hst_Opportunities
SELECT OppName, Oppvalue, #copyDate AS copyDate
FROM dbo.opportunities
the from part is what im struggling with - how do i copy cross server and database when the source database is MSSQL and the target is Azure SQL?
Based on my test, it appears you could create a linked server on your local box and insert the data using four part name:
EXEC master.dbo.sp_addlinkedserver #server = N'SQLAZURE', #srvproduct=N'sqlazure', #provider=N'SQLNCLI', #datasrc=N'NAME.database.windows.net', #catalog=N'TestDb'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'SQLAZURE',#useself=N'False',#locallogin=NULL,#rmtuser=N'USERNAME',#rmtpassword='Password'
GO
DECLARE #copyDate DATETIME = CURRENT_TIMESTAMP;
INSERT INTO [SQLAZURE].[TestDb].[Schema].[Table]
SELECT TOP (10)
[Column1]
,[Column2]
,#copyDate AS copyDate
FROM [Localdb].[Schema].[View];

How can I save a stored procedure which contains errors in SQL Server?

I want to save a stored procedure which contains errors according to SQL Server.
This is the procedure code:
Create PROCEDURE [Product].[JewelSearch]
#JewelItem bigint,
#JewelType nvarchar(50),
#JewelMate nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Product.#JewelType
WHERE Material = #JewelMate OR Item# = #JewelItem;
END
The problem is that I have a Product schema, and I am taking the table name from my main application and saving it in #JewelType and in each search in main application the table name must be changed and each time their will be a different table name in #JewelType.
According to me the query is perfect but SQL Server does not allow me to execute it and save it. Is there a way that I can forcibly save this stored procedure? Hope you understand my question please help me if possible.
If it is SQL Server, something like this should work
Create PROCEDURE [Product].[JewelSearch]
#JewelItem bigint,
#JewelType nvarchar(50),
#JewelMate nvarchar(50),
#SQL nvarchar(max)
AS BEGIN
SET NOCOUNT ON;
SET #SQL = 'Select * From Product.'+#JewelType+' where Material = '+#JewelMate+' OR Item# = '+CAST(#JewelItem as nvarchar(50))+'; '
EXEC(#SQL)
END
This is untested as I am on my Mac, but you get the idea.
If you are going to use this, be aware of the dangers of dynamic SQL in relation to SQL Injection.
SQL Injection with Dynamic SQL - MSDN

How to use dbo procedure to execute for different schema?

I am using SQL Server 2008 R2, I have a schema [dbo], and in that schema, I have created a stored procedure dbo.GetAccount:
SELECT * FROM tblAccount
Then, I have created a schema [ABC] with user named UserABC.
Now, I would like to login with UserABC and execute dbo.GetAccount for schema [ABC] to get all user of it and don't want to change code of dbo.GetAccount. So, how can I do?
Thanks for your help.
If I'm correct,
SELECT * FROM tblAccount actualy means SELECT * FROM [dbo].[tblAccount]
So your procedure will always execute the tblAccount from the dbo schema.
If you want ( let's say ) have a "general" procedure that can be execute from more schemas I say you try with dinamic sql.
CREATE PROCEDURE [dbo].[GetAccout]
#schema
as
declare #sql varchar(200)
set #sql = 'SELECT * FROM ['+#schema+'].[tblAccount]'
exec(#sql)
then you run exec [dbo].[GetAccout] ABC
This should select all info from YourSchema.tblAccount.
I can't test it right now, but I should work. Also remember the table must be in that schema!
But if you can't change all the the procedure I can't think of a solution.

Is it possible to create a table on a linked server?

Is it possible to create a table on a linked server?
exec
('CREATE TABLE DatabaseName.dbo.TableName
(
column1 datatype,
column2 datatype,
column3 datatype
)') at [LinkedServer]
The solution from Arpit works fine.
exec
('CREATE TABLE DatabaseName.dbo.TableName
(
column1 datatype,
column2 datatype,
column3 datatype
)') at [LinkedServer];
go
How ever, when you get the error "Msg 7411, Level 16, State 1, Line 1 Server 'MyLinkedServer' is not configured for RPC." you need to change the RPC-parameters on the linked server connection. As default RPC is set to false. It needs to be set to true.
This allows you to run procedures on the linked server.
You must allow this because the solution does not send the "Create table" statement as a SQL command to the linked server. It sends the statement as a string which in turn is executed as a procedure on the remote server.
Hope this helps.
If you're using SQL Server Management Studio and the SQLCMD mode:
-- To enable "SQLCMD mode" in SQL Server Management Studio (SSMS):
-- In the menu toolbar: Query > SQLCMD mode
:setvar LinkedServer "YourLinkedServerNameHere"
:setvar DestinationDb "YourDestinationDatabaseNameHere"
GO
EXEC ('
CREATE TABLE $(DestinationDb).dbo.YourTableNameHere (
[Field1] INT NOT NULL,
[Field2] INT NOT NULL
)
') AT $(LinkedServer)
GO
EXEC ('DROP TABLE $(DestinationDb).dbo.YourTableNameHere') AT $(LinkedServer)
GO

Resources