how to update multiple column in a single table - sql

I have created a table having three colums GsID,ALlowance and amount
Begin if exists(select * from [dbo].[HRAllowances] where GdId=#GdId) begin
update [dbo].[HRAllowances] set
Amount=#Amount where GdId=#GdId end
end
this only works for one particular row..
i want to allow user to update amount for all rows

After seeing your comment, if you wish to update all Amount fields for all records, remove your WHERE clause as this is filtering records by ones where GdId=#GdId:
UPDATE [dbo].[HRAllowances]
SET Amount=#Amount

If you want to update all the rows than you have to remove where condition that is
where GdId=#GdId from you query.
Like this :-
update [dbo].[HRAllowances] set Amount=#Amount
I hope it will works!!

Related

SQL Server - Select list and update a field in each row

I hate to ask something that I know has been answered, but after 45 minutes of searching I yield to everyone here.
I have a table where I need to select a list of items based on a value, and then take one field and add a year to it and update another column with that value.
Example:
select * from table where description='value_i_need'
UPDATE table SET endDate=DATEADD(year, 1, beginDate) -- For each item in the select
EDIT: The select statement will return a ton of results. I need to update each row uniquely.
I don't know how to combine the select or loop through the results.
My SQL knowledge is limited, and I wish I could have figured this out without having to ask. I appreciate any help
Edit 2:
I have one table that I need to go through and update the end date on each item that matches a description. So I have 1000 "Computers" for example, that I need to update a field in each row with that description based on another field in the same row. All coming from one table
Try like this
UPDATE
Table T1
SET
T1.endDate=DATEADD(year, 1, beginDate)
WHERE T1.description='value_i_need'
Unless I missed something, you should be able to simply add your criteria to your update statement as follows:
UPDATE [table]
SET endDate=DATEADD(year, 1, beginDate)
WHERE description='value_i_need'
UPDATE T
SET T.END_DATE = DATEADD(YEAR,1,T.BEGINDATE)
FROM TABLE T
WHERE T.DESCRIPTION = 'value_i_need
Do you mean:
UPDATE [TABLE] SET ENDDATE=DATEADD(YEAR,1,BEGINDATE) WHERE DESCRIPTION='VALUE_I_NEED'

How to Update a Single record despite multiple Occurances of the same ID Number?

I have a table that looks like the below table:
Every time the user loan a book a new record is inserted.
The data in this table is derived or taken from another table which has no dates.
I need to update this tables based on the records in the other table: Meaning I only need to update this table based on what changes.
Example: Lets say the user return the book Starship Troopers and the book return is indicated to Yes.
How do I update just that column?
What I have tried:
I tried using the MERGE Statement but it works only with unique rows of data, meaning you get an error if the same ID appears more than once.
I also tried using a basic UPDATE Statement and a JOIN but that's not going well.
I am asking because I have ran out of ideas.
Thanks for reading
If you need to update BooksReturn in target table based on the same column in source table
UPDATE t
SET t.booksreturn = s.booksreturn
FROM target t JOIN source s
ON t.userid = s.userid
AND t.booksloaned = s.booksloaned
Here is SQLFiddle demo
You can do this by simple Update & Insert statement.....
Two table A & B
From B you want to insert data into A if not exists other wise Update that data....
,First Insert into temp table....
SELECT *
INTO #MYTEMP
FROM B
WHERE BOOKSLOANED NOT IN (SELECT BOOKSLOANED
FROM A)
,Second Check data and insert into A.
INSERT INTO A
SELECT *
FROM #MYTEMP
And at last write one simple update statement which update all data of A. If any change then it also reflect to that data otherwise data as it is.
You can also update from #MYTEMP table.

Delete a specific field from a database

How can I delete the value of a field from a database, using query code? I only know where the field is located in the database. (ex: column['phone number']row[3])
It should be something like this "DELETE FROM ... WHERE ..."
There is nothing like delete the field in database i.e. if you want to remove the complete row then you can do that easily like this
DELETE FROM TABLE WHERE COLUMN1=#myValue
But if you wanted to remove one value of the row in that case you should update that value as NULL
UPDATE TABLE SET COLUMN1=NULL WHERE COLUMN1=#myValue
I have used where clause from my imagination. you can always use whatever you want.
The DELETE statement allows you to delete RECORDS.
When you want to set a field to an empty value, use the UPDATE statement.
See this for more info.
UPDATE [YOUR_TABLE] SET [YOUR_FIELD] = NULL WHERE [YOUR_PRIMARY_KEY] = VALUE
Yes first you need to use delete command. Then after this insert update command then only the records will be deleted from the database. I agree that you have deleted records but its not showing because you haven't updated it.

SQL update function doesn't throw any error message but it returns no rows

I am trying to update a table value from another table. I want to update pstatus from productcode.
Here is my code (which gives no error and results 0 rows):
UPDATE pstatus
SET code=(select code FROM productcode)
besides this, I can't re-run any update in sql - it gives no error, but returns 0 rows (I have more than 5 rows in productcode table).
In this case you have a two problem
you say you dont have any rows in pstatus if no record in table how can table row update ?
your inner query select code FROM productcode return more then one row then how sql decide to set with value in wich field?
An update statement doesn't return rows. You should have used something more like
Update pstatus
Set code=a.code
from productcode a
where a.*somekeyfield* = *value*
You have to determine which single record from productcode you wish to use as the basis for the update. What somekeyfield should be is something you need to determine.
An update statemend doesn't add records, it only updates records that already exist in the table.
The update query does actually udpate all the records in the table, but as there are no records, the number of updated records is zero.
If you want to add records, you should use an insert query instead:
insert into pstatus (code)
select code from productcode

Convert value in all rows to VARBINARY(50)

I want to upgrade all columns in a table, what I am aiming to do is retreive a column from the row it will update then update it, something like:
update works
set encrpyted_item_no = (CAST(RTrim((
select unencrypted_item_no
from works
where name = name
) AS VARBINARY(50))
I know that query is wrong, it's just an example to show you what I am aiming to do.
I want it to select the column unencrypted_item_no from its row then update that same row with the data it gets from unencrypted_item_no, doing this for the whole table.
How would I accomplish this?
You shouldn't need to do a sub-select, referencing the other column in the set will work on a row by row basis, ie:
UPDATE works
SET encrpyted_item_no = CAST(RTrim(unencrypted_item_no) AS varbinary(50))
Shouldn't this be enough ?
update works
set encrypted_item_no = CAST(RTrim(unencrypted_item_no) AS VARBINARY(50))