Compose a Sql query with multiple variables - variables

My SQL query:
update table_name
set column_name = replace (column_name, 'old_string' ,'new_string')
where column_name like 'old_string'
I have a gui from which users select column for modify a string and two entry for replaced string and replace string
My variables:
self.column_name for column selected
self.old_string for string to replace
self.new_string for replaced string
How can I make my SQL command to work with multiple variables in many places in Python?
Thanks

Related

SSIS Execute SQL Task contain Insert Into and Select

I am attempting to read file names then input each name in a row with another similar cell value to another table column. Within the For Each Loop, then I have the Execute SQL Task. currently the Task is set to Direct Input. This is the problem script
INSERT INTO TableB
(LoopValue, Columnvalue)
VALUES (?, N'Select Columnvalue from TableA where Columnvalue like ?')
You have a for each loop over files, as I understood it? When I have these kind of situations I simply map the current file to a fileName variable using the "Variable Mapping" setting of the foreach loop, using index 0.
Then I create a Data Flow task with a derived column which is simply your fileName variable. Using this in conjunction with an OLE DB destination ought to be sufficient to insert the value into a table, if I understand you correct?
EDIT:
Oh you want to create a string as well? You could achieve that in your Control Flow using an Expression Task. Assign a string variable, such as "queryString" and then have:
#queryString = "Select " + #[User::fileName] + " from Table B"
or something in the lines of that, i.e. use "" to insert "hard coded" values and a + followed by your variable to concatenate your variable. You could then also use a derived column for your #queryString variable in your data flow as well.
You're close if I understand what you are attempting to do which is insert into TableB selecting records from TableA where ColumnValue is like the passed in value.
The way you have it now it wants to insert the literal string of Select Columnvalue from TableA where Columnvalue like ? into your table.
Tweak your statement like this:
INSERT INTO TableB (LoopValue, Columnvalue)
SELECT ?, Columnvalue FROM TableA WHERE Columnvalue LIKE '%' + ? + '%' --% is a wildcard. If you only want those that begin or end then remove one or the other %
Then make sure you're passing in the value twice. Since there are multiple ? that relates to the number of parameters, but you can pass it twice since you are using the same value in two different places:
Parameter names would then start with 0 and increment up to match the ? in your statement. Depending on your connection type that could be different, there is a section in the following documentation that tells you what it should be Execute SQL Task

Comparing 2 strings and find out mismatches

I need to create a query in MariaDB 10.2. I am trying to create a query with string functions as now
Below is the scenario
One string has placeholders and another string has values of those placeholders. I want to get a tabular output in which one column have placeholders and another column have placeholder's values
value1 -->
jdbc:postgresql://$PGHOST_1$:$PGPORT_INSTANCE_1$/eventstore
value2 -->
jdbc:postgresql://1.2.3.4:5432/eventstore
now i want to get below data
Thanks in advance.
This needs a looping mechanism. Therefore, use a stored procedure or client code to do the looping:
SELECT placeholder, `value` FROM mylist;
`col` starts out looking like `value1`
foreach row in the list of replacements:
UPDATE mytable SET col =
REPLACE(col, placeholder, `value`);
Now `col` looks like your `value2`
or, if you don't want to change col:
SELECT placeholder, `value` FROM mylist;
SELECT #col := col FROM mytable;
foreach row in the list of replacements:
SET #col =
REPLACE(#col, placeholder, `value`);
then use #col in subsequent work
(value is a keyword, hence the backtics.)
From there, build the "mismatches".

Appending character to variable in execute query

I want to execute a dynamic SQL statement, which searches for names whose last name is always a constant and first name is a variable.
Here is a query I have written for selecting a row with name='Test lastname'.
EXECUTE 'SELECT name FROM users
WHERE name=$1 lastname'
USING ('Test');
This generates a syntax error. Is it possible to do this?
I think you need something like this:
EXECUTE 'SELECT user_id FROM users
WHERE name=$1'
USING Test||' lastname' ;
Here Test is variable and 'lastname' is hard coded value
Also another way is as #JorgeCampos mentioned:
...WHERE name=$1 || '' lastname''' USING 'Test';

how to avoid reaching max size of varchar2 in concat with update

I am new to oracle. when updating a table i am using
update my_table set column_name =concat(" ' ", column_name, "viewed by xxxx")
where item_value=2.
this value is being appended whenever some one is viewing...
Now my question is How to avoid max size of varchar(2) and set if it is just 50 characters lesser than its size. I am using this in servlet
My requirement is ..If some user views the item_value of 2, his userid is to be entered in my_table by concatinating the existing values in column_name where item_value=2, in one word I dont want to loose the earlier data in field when updating and to avoid reaching the max size of the field which is varchar2(900).
If the field reaches max size then the existing data will be ovewritten by the new data else it will be a concatination to existing data.
Just use SUBSTR:
update my_table set column_name
= SUBSTR(' '' ' || column_name || 'viewed by xxxx')
, 1, 900)
where item_value=2
P.S. concat can only take 2 arguments - I've converted your code to use the simpler || concatenation operator instead.
P.P.S. string literals in Oracle are delimited by ' not "
You should create a stored procedure that you will call to update that field. It will check what the new size will be after the update, and if it is too large then perform whatever action you need to (ie. add to archive table, trim off existing data, etc.).

How to include double quotes in select statement?

i have a table like
id name
10 bob
20 bill
i want to select only name column in output with double quotes
like select '"'||name||'"' from table
it is giving me the correct output but is there any other way without using concatenation ...
Thank you..
Using this you can get result with double quotes
' " ' + columnName + ' " '
Example
Query
SELECT '"'+Name+'"' , Age
FROM customer
Result
"Viranja" | 27
Create a virtual column that adds the quotes:
CREATE TABLE
....
quoted_name VARCHAR2 GENERATED ALWAYS AS ('"' || name || '"') VIRTUAL,
...
See here for more information:
http://www.oracle-base.com/articles/11g/virtual-columns-11gr1.php
this will check for any name with at least one double quote
select * from table
where name like '%"%'
If your intention is to be able to "export" the result into space or comma-delimited text file, use a view to "format" your data. You will need to this for your date columns as well.
There are two scenarios you would want to use double quotes in sql in my opinion.
Updating a string column which contains single multiple quotes in it. (you have to escape it)
updating blog contents in columns which you cant edit in "edit top 200 rows"
so, if you want to use double quotes follow this.
SET QUOTED_IDENTIFIER OFF
BEGIN
DECLARE #YourSqlStmt AS VarChar(5000) -- Declare a variable.
SET #YourSqlStmt = "ok"
PRINT #YourSqlStmt -- do your operations here
END
This saves time and you need not to escape single quotes in a existing string content of the column.