How to insert '&user' in oracle column? [duplicate] - database

This question already has answers here:
How do I ignore ampersands in a SQL script running from SQL Plus?
(7 answers)
Closed 7 years ago.
One of my oracle database columns needs to have the value 'user.password&user.user_name'
When I execute the query, I see a popup, because the query has '&user' in it. How do I get around it?
Example query:
update Table A set value = 'user.password&user.user_name' where id = 1;

Modify the query to insert the value
'user.password'||chr(38)||'user.user_name'
Example query:
update Table A set value = 'user.password'||chr(38)||'user.user_name' where id = 1;

You can find several decisions at How do I ignore ampersands in a SQL script running from SQL Plus?
Btw, you can use just update table set value = &value where id = 1 and type your value into popup.

chr(38) should work independent of the platform.
However, if you are using SQL*Plus, then you could use the sqlplus command:
set define off
In SQL*Plus, ampersand is used for substitution variable. The above command will ignore it.
For example,
SQL> SET DEFINE OFF
SQL> SELECT 'user.password&user.user_name' FROM DUAL;
'USER.PASSWORD&USER.USER_NAM
----------------------------
user.password&user.user_name
SQL>
Well, of course,chr(38) will work too:
SQL> SELECT 'user.password'||chr(38)||'user.user_name' FROM dual;
'USER.PASSWORD'||CHR(38)||'U
----------------------------
user.password&user.user_name
SQL>

Related

What kind of T-SQL table hint should I use in the SELECT before an INSERT statement? [duplicate]

This question already has answers here:
Upsert with concurrency checking SQL Server
(1 answer)
Insert if not exists avoiding race condition
(2 answers)
Closed 7 months ago.
I have a SQL query as below, running within REPEATABLE READ transaction.
IF NOT EXISTS (SELECT * FROM TableName WHERE Column1Name = #Column1Value)
INSERT INTO TableName VALUES (#Column1Value, #Column2Value);
The above query gets run within multiple processes. I see a possibility when process 1 does a SELECT and then process 2 also does as SELECT (as there is shared lock) before either of the processes get chance to do an INSERT, there would be a deadlock. I would like to avoid this. What is the correct table hint to use for this situation? Note that it is not possible to enable READ_COMMITTED_SNAPSHOT on the database.
I would not use a hint but instead do this in one operation. Check out Davide's article and try and apply his method with the single merge statement. In my opinion the preferred method.

Best alternate of SQL Server rowversion (IROW_VERSION) in PostgreSQL? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have a row version column in SQL Server table. At the time of update and insert command auto generate value automatically inserted/updated in this column. I need same in PostgreSQL. One option is to use function trigger with sequence generation but it may cause locking issue. What is the best practice/alternate in PostgreSQL.
The question is somewhat unclear. rowversion in SQL Server is only used as a concurrency token in optimistic concurrency scenarios. This is faster than a trigger that updates a LastModified timestamp or increments a stored column.
The equivalent in PostgreSQL is the system-provided xmin column:
xmin
The identity (transaction ID) of the inserting transaction for this row version. (A row version is an individual state of a row; each update of a row creates a new row version for the same logical row.)
Essentially, for a single row, xmin always changes after every modification, just like rowversion does. It's faster than a trigger too, since it requires no extra effort.
The NpgSQL provider for Entity Framework uses xmin as a concurrency token.
If you want to implement optimistic concurrency manually, read the xmin column in your SELECT statement and use that value in updates, eg:
SELECT xmin, ID, Name FROM sometable;
Which returns
xmin | ID | name
------+----+------
123 | 23 | Moo
And then
UPDATE sometable
SET name = 'Foo'
WHERE ID = 23 AND xmin = 123
If the row was modified by some other transaction, xmin won't match and no changes will be made. You can detect that by checking how many rows were changed using your provider's API. That's how rowversion works too.
Another possibility mentioned in the linked question is to use the RETURNING clause to return some value to the client. If no value is returned, the statement failed, eg:
UPDATE sometable
SET name = 'Foo'
WHERE ID = 23 AND xmin = 123
RETURNING 1
Not sure what "locking issue" you are talking about, but you can get something equivalent (assuming I understood the "row version" thing correctly) without a sequence:
create table some_table
(
.... columns of the table ...,
row_version bigint not null default 0
);
Then create a trigger function:
create function increment_row_version()
returns trigger
as
$$
begin
new.row_version := old.row_version + 1;
return new;
end;
$$
language plpgsql;
By using the old record, it's impossible to overwrite the row version value in an UPDATE statement.
And then create the trigger for every table where you need it:
create trigger update_row_version_trigger
before update on your_table_name_here
for each row
execute procedure increment_row_version();
If you also want to prevent inserting e.g. a higher number as the start number, you can extend the trigger to run on insert and update and in case of an insert assign the desired start value explicitly.
If you need a global value across all tables (rather than one number for each table as the above does), create a sequence and use nextval() inside the trigger rather than incrementing the value. And no, the use of a sequence will not cause "locking issues".
The trigger would then look like this:
create function increment_row_version()
returns trigger
as
$$
begin
new.row_version := nextval('global_row_version_sequence');
return new;
end;
$$
language plpgsql;
and can be used for both insert and update triggers.

SQL Server deleted records even though there was an error in the subquery which located in where clause with IN tag [duplicate]

This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 8 years ago.
As you see from these images, the ROSHEADERID column is invalid and when I execute the first one it returns a meaningful message. But when I use this query in where clause as a subquery. It executes and deletes all of the records without warning or aborting the operation.
How can this be ?
The subquery has access to columns in outer query, thus column ROSHEADERID you ask for is effectively taken from EXM_REVIEWOFSYSTEMS (not from EXM_REVIEWOFSYSTEMSHEADER), thus deleting all records in outer table.
This should clarify a bit on what's going on behind the scenes:
http://sqlfiddle.com/#!2/623c7/3
More information here: http://technet.microsoft.com/en-us/library/ms187638(v=sql.105).aspx
Use Alias Names for tables to avoid conflict:
BEGIN TRAN
DELETE FROM EXM_REVIEWOFSYSTEMS
WHERE ROSHEADERID IN ( SELECT rsh.ROSHEADERID
FROM EXM_REVIEWOFSYSTEMSHEADER rsh
WHERE rsh.PATIENTID = '' )
ROLLBACK TRAN

Transact-SQL using variable for table name [duplicate]

This question already has answers here:
A table name as a variable
(10 answers)
Closed 9 years ago.
I have try to use statement below in my SQL. Is it possible by any means to dynamically select table form database. (For example select table name from Comobox and then display data from selected table)
SET #var1 = 'test';
SELECT * From #var1
Dynamic SQL requires that you create a string consisting of the sql statement, and then you execute the string. Dynamic t-sql quotes in string

"Select * from ..." doesn´t work, "select code, descr from..." does, why?

I run this query on SQL Server and it doesn't work:
SELECT * FROM dbo.marcas
but if I put at least one field in the query, it works.
SELECT code FROM dbo.marcas
I know it must be simple, but I can't find an answer.
Thansk
Most likely, someone else is updating that same table, and thus places certain locks on the table.
When you do a SELECT * ... those locks will cause a conflict and your query won't execute, while a SELECT (list of columns)...... will work (since it's not affected by the locks)
I'm answering my own question because I have found the answer by myself.
Using EMS Sql Manager 2008 for SQL Server I executed select * from marcas and have no results, just errors. But If I recreated the table, voila, it just worked fine !!!
So the problem was the way I created the tables in the server. After a while, I realized the command that created the table in Foxpro using ODBC was:
oerr = sqlexec(oconn, "ALTER TABLE ["+xtabla+"] ADD ["+borrar.field_name+"] "+tipo_campo(borrar.field_type, borrar.field_len, borrar.field_dec),"")
so changed it to:
oerr = sqlexec(oconn, "ALTER TABLE ["+xtabla+"] ADD ["+alltrim(borrar.field_name)+"] "+tipo_campo(borrar.field_type, borrar.field_len, borrar.field_dec),"")
that is, I just deleted the extra spaces right after the table name.
Thats all, "codigo" is not equal to "codigo ".
Thanks to all of you who tried to help me.
I beleve
One possibility would be if you have a computed column in the table that's generating an error when SQL Server attempts to compute it. Sample code:
create function dbo.Crash ()
returns int
as
begin
return 1/0
end
go
create table dbo.cctest (
Col1 int not null,
Col2 int not null,
CrashCol as dbo.Crash()
)
go
insert into dbo.cctest (Col1,Col2)
select 1,2 union all
select 3,4
go
select Col1 from dbo.cctest
go
select * from dbo.cctest
go
results:
Col1
----
1
3
(2 row(s) affected)
Col1 Col2 CrashCol
--------------------
(2 row(s) affected)
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.
So the first select worked since it didn't access the fault computed column
I recommend running the query in a sql client other than EMS, in the hope that you can get an informative error message.
"La operación en varios pasos generó errores. Compruebe los valores de estado." -> "The multi-step operation generated errors. Check the status values."

Resources