SQL UPDATE with database values - sql

It is possible to UPDATE sql column with the actual values ?
EXAMPLE :
UPDATE TEST SET example = {actual_value}, other = 'test'
I want to replace {actual_value} with the actual value without ask sql server before update.

Ok, found the answer thanks to #muasif80.
IF you want to update with actual value :
UPDATE TEST SET column_name = column_name

Related

How to add text in the beginning of each record in a row in sql

i have the following data set in the leads column:
**leads**
1234567
1234534
1234543
i want it to be updated as
**leads**
LEA-1234567
LEA-1234534
LEA-1234543
You can use concat.
select
concat('LEA-', leads) as lead
from yourTable
Maybe you can try to use update script directly
update table set table.leads = concat('LEA-', table.leads)
UPDATE yourTable
SET leads = "LEA-" || leads
Update tablename
set leads='LEA-'+leads

MS SQL use IDENT_CURRENT as variable of query

I need to use the IDENT_CURRENT of a specific table inside a query I'm writing.
I don't want to do a query to store the ID and another query to use it. I'd like something like:
UPDATE my_table1
SET my_column1 = IDENT_CURRENT(my_table2)
WHERE my_column2 = ?
Is it possible?
Yes, this code will work fine.
Table name for the function IDENT_CURRENT must be in quotas:
UPDATE my_table1
SET my_column1 = IDENT_CURRENT('my_table2')
WHERE my_column2 = ?
Needs quotes around table name.
UPDATE my_table1
SET my_column1 = IDENT_CURRENT('my_table2')
WHERE my_column2 = ?

updating a field where field cannot be less than zero

im using the following query to update my field
UPDATE tableName SET fieldName = fieldName-10
WHERE id=1;
and this is working fine in my case but i want that if the result is less than zero than query should not execute.
i've tried this
UPDATE tableName SET fieldname = fieldName-10
WHERE id=1 and fieldName>0;
but it returns with an error syntax error in your UPDATE statement.
thanx in advance
did you mean this? if pqty is the field of your table.
UPDATE tableName SET fieldname = pqty-10 WHERE id=1 and pqty>10;

Need example of Conditional update stored proc in SQL server

I am just at starting levels in DB usage and have 2 basic questions
I have a generic UPDATE stored proc which updates all columns of a table.
But i need to make it conditional wherein it does not SET when the parameter is NULL.
Usage: I want to use this as a single SP to UPDATE any subset of columns, the caller from C# will fill in corresponding parameter values and leave other parameters NULL.
2
In case of , "UPDATE selected records" do i need to use locking inside stored proc ?
Why ? Isn't the operation in itself locked and transactional ?
I find the same question come up when i need to UPDATE selected(condition) records and then Return updated records.
UPDATE table
SET a = case when #a is null then a else #a end
WHERE id = #id
OR
EXEC 'update table set ' + #update + ' where id = ' + #id
OR
Conditionally update a column at a time
First option to me would usually be preferrable as it is usually efficient enough and you do not need to worry about string escaping
If I have understood the question properly, Why can't you build a query on the fly from sql server SP, and use sp_sqlexecute. So when you build query you can ensure only columns that have value has got updated.
Does this answer your question?

OPENQUERY update on linked server

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'`.