Update One Colum With Other Column Value In Same Table Using Update Table Statement

Jun 14, 2007

Hi,
I have table with three columns as below
table name:exp
No(int) name(char) refno(int)

I have data as below
No name refno
1 a
2 b
3 c

I need to update the refno with no values I write a query as below

update exp set refno=(select no from exp)
when i run the query i got error as
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.

I need to update one colum with other column value.
What is the correct query for this ?

Thanks,
Mani

View 3 Replies


ADVERTISEMENT

When Then Muliple Colum Update 1 Statement

Apr 8, 2008

I have about 5 statements like the update below, depending on the PID different columns will be update "C2005, G2005,E2005...."

I would like to use 1 update statement in stead of 5 to update all columns below are 2 original update statements and my attempt at when then update. Note a different column is updated depending on the PID.

If when then isnt possible, any other suggestions are welcomed. Thanks
UPDATE #Sec
SET C2005 = Pos.USD / 1000
FROM #Sec INNER JOIN
Pos ON #Sec.ID = Pos.ID
WHERE (Pos.PID = 'B')

UPDATE #Sec
SET G2005 = Pos.USD / 1000
FROM #Sec INNER JOIN
Pos ON #Sec.ID = Pos.ID
WHERE (Pos.PID = 'G')

UPDATE #Sec
WHEN (Pos.PID = 'C') THEN SET C2005 = Pos.USD / 1000 end
WHEN (Pos.PID = 'G') THEN SET G2005 = Pos.USD / 1000 end
WHEN (Pos.PID = 'E') THEN SET E2005 = Pos.USD / 1000 end
FROM #Sec INNER JOIN
Pos ON #Sec.ID = Pos.ID

View 3 Replies View Related

SQL Server 2012 :: Update Table From Variable Table Column?

Oct 6, 2014

I am trying to use a stored procedure to update a column in a sql table using the value from a variable table I getting errors because my syntax is not correct. I think table aliases are not allowed in UPDATE statements.

This is my statement:

UPDATE [dbo].[sessions_teams] stc
SET stc.[Talks] = fmt.found_talks_type
FROM @Find_Missing_Talks fmt
WHERE stc.sessionid IN (SELECT sessionid FROM @Find_Missing_Talks)
AND stc.coupleid IN (SELECT coupleid FROM @Find_Missing_Talks)

View 2 Replies View Related

Update One Table With Value Of A Column In Another Table Plus A Constant

Sep 19, 2012

Need to update one table with value of a column in another table plus a constant:

UPDATE TABLE_A
SET TABLE_A.COLA=TABLE_B.COLB+'10'
FROM TABLE_A
INNER JOIN TABLE_B
ON TABLE_A.COLA=TABLE_B.COLA
WHERE TABLE_A.COLA=TABLE_B.COLA

The above statement works except the concatenated string has space between TABLE_B.COLB text and '10'. How to remove the space (4 characters) so that the string shows as one word 'TABLE_B.COLB10'?

View 2 Replies View Related

T-SQL (SS2K8) :: Update One Column For One Table From Another Table?

Jul 8, 2014

I have to tables say 'employee1' and 'employee2'

employee1 has lastname
employee2 has firstname, lastname

i have to update firstname in employee1 from firstname in employee2 table and the common field for both tables is 'lastname'

View 8 Replies View Related

Update Table Column Based On Value From Another Table?

Sep 2, 2005

Hi, I have two tables. I want to update two columns in my first table,[ADD_BSL_SALES] and [ADD_BSL_COST] with two values [Sales] and[Costs] held in my #temp table but based on a RUN_DATE from my firsttable.Can anyone point me in the right direction?Thanks in Advance ï?ŠBryanCREATE TABLE [GROSMARG_AUDIT_ADDITION] ([RUN_DATE] [datetime] NULL ,[SALES_DIFF] [numeric](19, 6) NULL ,[COST_DIFF] [numeric](19, 6) NULL ,[ADD_BSL_SALES] [numeric](18, 0) NULL ,[ADD_BSL_COST] [numeric](18, 0) NULL ,[ADD_SALES_DIFF] [numeric](18, 0) NULL ,[ADD_COST_DIFF] [numeric](18, 0) NULL) ON [PRIMARY]GOINSERT RUN_DATE,datetime,INSERT SALES_DIFF,numeric(19,6),INSERT COST_DIFF,numeric(19,6)INSERT ADD_BSL_SALES,numeric(18,0),INSERT ADD_BSL_COST,numeric(18,0),INSERT ADD_SALES_DIFF,numeric(18,0)INSERT ADD_COST_DIFF,numeric(18,0)--- Second TableCREATE TABLE #DUPTOTALS[Sales][Costs]

View 1 Replies View Related

Update A Table By Copying A Column From Another Table

Jul 20, 2005

I need to update a table by copying a column from another table(having the samestructure, but on another database), from the record having the sameprimary key.1 - What is the correct query?2 - I tried copying them record by record, but the datatype is ntext,(it displays <longtext> in the result pane), and trying to update it results in thefollowing errormessage:The text, ntext, and image data types are invalid in this subquery oraggregateexpression.I tried variations of the following:UPDATE TABLESET column0 = (SELECTcolumn0FROManotherDB.dbo.TABLEWHEREanotherDB.dbo.TABLE.column1 = column1)WHEREanotherDB.dbo.TABLE.column1 = column1

View 1 Replies View Related

Update Table A With Data From Table B Without Specifying Every Column

Mar 25, 2008

hello all,

I don't know how to update table A with data from table B without specifying every column.
These two tables have the same fields and same structure.

I know that it's possible to do the following:

update table A

set A.name = B.name,

A.job = B.job
from table B

But I have many columns and don't want to describe every column, is that possible?

Thanks!

View 6 Replies View Related

Update Table With Case Statement

Aug 31, 2007

can plz anyone tell me how to fix the following update script. thanks

Update table
set rating =
case
when rating in
(select code from tbl_Codes
where code = '0')
then Rating = '0'

when Rating in
(select code from tbl_Codes
where code = '1')
then InternalRating = '1'
else rating
end

View 5 Replies View Related

Compare Two Table And Update - Join Statement

Apr 27, 2015

I compare two table with JOIN statement. Now I would like to update one of them base on result. How it to do?

View 2 Replies View Related

Easy Table Based Update Statement???

Jul 20, 2005

Hello,I have 2 ways of updating data I'm using often1) via a cursor on TABLE1 update fields in TABLE22) via an some of variables ...SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE FLD-ID = @inputVARUPDATE TABLE2SET FLDx = @var1, FLDy = @var2WHERE ...Now I have a system with 2 databases and I need to update table DB2.TABbased on data in DB1.TAB. Instead of using 1 of the 2 ways I normally use,I thought it would be much easier to get the required data immediately fromDB1.TAB in the update-statement of DB2.TAB ... but the way to do thatconfuses me. I've checked books online and a lot of newsgrouppostingsgiving good information but still I keep getting errors like this ...The column prefix 'x.ADS' does not match with a table name or alias nameused in the query.while executing the following statement ...UPDATE DB2.dbo.TABSETFLD1 = x.FLD1,FLD2 = x.FLD2,...FROM DB1.dbo.TAB x, DB2.dbo.ADSWHERE DB2.dbo.TAB.REFID = x.IDOFTAB1 AND DB2.dbo.TAB.IDOFTAB2 =@InputParameterSo in DB2.TAB I have a field REFID reffering to the keyfield IDOFTAB1 oftable DB1.TABAND I only want to update the row in DB2.TAB with the unique keyfieldIDOFTAB2 equal to variable @InputParameterDo you see what I'm doing wrong?--Thank you,Kind regards,Perre Van Wilrijk,Remove capitals to get my real email address,

View 8 Replies View Related

Update Column From Another Table

Sep 25, 2007

Is it possible to update from one table to another?Pls examine my code here:
UPDATE tStaffDir SET tStaffDir.ft_prevemp = ISNULL(tStaffDir_PrevEmp.PrevEmp01, ' ') + ' ' + ISNULL(tStaffDir_PrevEmp.PrevEmp02, ' ') + ' ' + ISNULL(tStaffDir_PrevEmp.PrevEmp03, ' ') + ' ' + ISNULL(tStaffDir_PrevEmp.PrevEmp04, ' ') + ' ' + ISNULL(tStaffDir_PrevEmp.PrevEmp05, ' ') Where tStaffDir_PrevEmp.ID=tStaffDir.ID
I am trying to concatenate the columns from tStaffDir_PrevEmp to tStaffDir but I have this error where tStaffDir_PrevEmp is recognised as a column and not a table.
Pls advise how this can be done. Many Thanks.

View 3 Replies View Related

Update A Column In Every Row Of The Table

Jan 30, 2007

I have a table wherein I have to update a particular column (receipt code) based on another column of the same table (receipt number). i have to do calculations in order to generate the correct receipt code, and i have to do this on every row of the table. how can i do this? will this update be in a loop or something?

View 6 Replies View Related

Update A Column In A Table?

May 13, 2014

I need to write a update statement which will update the same table with value from the same column but the values will be changing.

For an example.

The source table is as below.

ROWID DATA Header
1 AAA H1
2 isa1
3 isb2
4 AAA3b3 H2
5 same

Now the records with rowid 2 and 3 should be updated with H1 as Header which is present in the Rowid 1.

Similarly the record rowid 5 should be updated with H2 as header from rowid 4.

View 5 Replies View Related

Update XML Column In Table

Jan 2, 2014

I am trying to remove a user from Reporting Services via code. I have found that the way to do this is to remove their details from an XML field in the table SecData

The name of the column is xml_description

An example value of the column is below:

<Policy>
<GroupUserName>BUILTINAdministrators</GroupUserName>
<GroupUserId>AQIAAAAAAAUgAAAAIAIAAA==</GroupUserId>
<Roles>
<Role>
<Name>Content Manager</Name>

[Code]....

I would like to remove the bolded part in the column when I put in the user's name.

So the code would start with:

Declare @username varchar (20)
Set @ username = 'EMEAMJohnson'

Followed by the logic to remove the bolded part of the column.

The column is called xml_description and the name of the table is SecData.

View 17 Replies View Related

Update Column From Another Table

Apr 26, 2007

Hello all, was wondering if you could point me in the right direction for this.

I have a db with a column classifications, and in that column are 'Accountants - (1234)' 'Book-keepers - (18) etc etc. Now what I want to do is remove the - (xxxx) section and obviously the white space, so I am just left with 'Accountants' 'Book-keepers' etc.

1. Is there an easy way to do this?

Ok so my thoughts were create a another table and put the ammended classifications in that to update the first table. Your probably asking why not just do it on the first table...Answer. There are over 150,000 records to change.

So I now have my first table with column classification and my second table with the correct classification ammendments.

I can sit down and manually type

UPDATE table1
SET classifications = 'Accountants'
Where classifications = 'Accounts - (xxxx)'

until i have completed the entire list in table 2 but I was hoping you good people would know a way to work through the list so it would automatically update each classification correctly.

The problem I have here is I dont know how to work through table 2 and match it to something in table 1 and update it.

Any help is greatfully appreciated

View 16 Replies View Related

Update A Column / Other Table

Apr 11, 2008

Hi,

I am quite new in SSIS and I have a question. I don't know what is the best way to get it work...

So simply I have in my SQL Server database 2 tables :

First table "SER" fields
SER_ID
SER_IS_CHANGE_RELATED

where I have only the field SER_ID filled like
SER_ID SER_IS_CHANGE_RELATED
1234 NULL
1235 NULL
1236 NULL

Second table "SRE" fields
SRE_ID
SRE_SER_ID
SRE_CHA_ID
and the content would be for example
SRE_ID SRE_SER_ID SRE_CHA_ID
1 1234 2345
2 1234 2346
3 1236 2347

The 2 tables are related by the fields : SER_ID = SRE_SER_ID

So I would like to have the following result in my table "SER":
SER_ID SER_IS_CHANGE_RELATED
1234 True
1235 NULL
1236 True

"True" because one or more entry is present in table "SRE" for each SER_ID (= SRE_SER_ID)


Hope my example is clear....
Thanks for your help

View 3 Replies View Related

Performance Table - Update Statement For 13 Week Average

Oct 16, 2013

I need to figure out the correct update statement syntax for the following integration.

I have a "Performance Table" which i insert weekly performance numbers into for each store. The table is constructed w/ columns such as Store, Weekenddate, Sales, Refunds, #ofPatients

In a "Averages Table" i have every weekenddate for each store populated. So 52 Weeks for 10 stores = 520 Rows of Store numbers & WeekendDates.

What i would like to do is run a loop or update statement which would update the store average for each weekendate based on the last 13 weeks.

This is my query

update performancestore_avgs set SalesAvg =
(select sum(SalesHit)/Count(Store) from performance_store where performance_store.weekenddate >= performancestore_avgs.weekenddate-84 and performancestore_Avgs.store = performance_store.store)

The update statement runs but the averages are completely wrong.

View 3 Replies View Related

Update Multiple Columns In One Table With Case Statement

Nov 15, 2013

I want to update multiple column in one table using with case statement. i need query pls..

stdidnamesubject result marks
1 arun chemistry pass 55
2 alias maths pass 70
3 babau history pass 55
4 basha hindi NULL NULL
5 hussain hindi NULL nULL
6 chandru chemistry NULLNULL
7 mani hindi NULLNULL
8 rajesh history NULLNULL
9 rama chemistry NULLNULL
10 laxman maths NULLNULL

View 2 Replies View Related

UPDATE Records In 1 Table With Result Of Select Statement

Jun 12, 2014

I want to update records in 1 table with the result of a select statement.

The table is called 'MPR_Portfolio_Transactions' and contains the following fields:

[PTR_SEQUENCE]
,[PTR_DATE]
,[PTR_SYMBOL]
,[PTR_QUANTITY]
,[PTR_ACUM]

And the select statement is like this:

SELECT SUM(PTR_QUANTITY) OVER (PARTITION BY PTR_SYMBOL ORDER BY PTR_DATE, PTR_SEQUENCE) AS 'ACUMULADO'
FROM MPR_portfolio_transactions
ORDER BY PTR_SYMBOL, PTR_DATE, PTR_SEQUENCE

This select statement generates one line per existing record. And what I would like to do next is to UPDATE the field 'PTR_ACUM' with the result of the 'ACUMULADO'

the key is PTR_SEQUENCE

View 3 Replies View Related

Prolonged Execution For Table UPDATE Statement - Not Sure It Finishes

Jul 27, 2006

I am cleaning up a large database table that has Date keys instead of real DateTimes. To do this, I am running the following query...

UPDATE MQIC.DBO.OBSERVATION_F

SET MQIC.DBO.OBSERVATION_F.OBS_DATE = MQIC.DBO.DATE_D.ACTUAL_DATE

FROM MQIC.DBO.OBSERVATION_F INNER JOIN MQIC.DBO.DATE_D

ON MQIC.DBO.OBSERVATION_F.DATE_KEY = MQIC.DBO.DATE_D.DATE_KEY

where Actual_Date is what is being stored, and the Date_Key is to be dropped.



The particulars are this -

Date_D table - 92,000 rows - 40 MB

Observation_F - 2,000,000 rows - 3.2 GB

This is being run on a remotedly hosted rack server with an AMD processor, 1 GB RAM, 60 GB harddisk space, 20 GB used.



SQL-Server 2005 Express - SP1



If I do the same query as a SELECT Statement,



UPDATE MQIC.DBO.OBSERVATION_F

SELECT MQIC.DBO.OBSERVATION_F.DATE_KEY, MQIC.DBO.DATE_D.ACTUAL_DATE

FROM MQIC.DBO.OBSERVATION_F INNER JOIN MQIC.DBO.DATE_D

ON MQIC.DBO.OBSERVATION_F.DATE_KEY = MQIC.DBO.DATE_D.DATE_KEY

it runs to completion in about 15 min - during the entire time there is extensive used of CPU from Task Manager.

If I do the above statement, it seems to use lots of resources (50% +) for about 5 min, then falls to 5%. It just seems to sit there, for an hour + at which time I've killed the query.

This is actually the second time I tried this. The first time was on a different machine, with the P4, 3GB RAM, plenty of disk space, and using SQL-Server 2005 Standard - SP1. Exactly the same decrease in resources happened, and even though it ran several hours, no results.

Any thoughts here - not waiting long enough, memory leaks, etc.?

Thanks!

View 6 Replies View Related

Update Table With Value Of Another Tables Column?

Sep 6, 2007

How do I update a table's column, with the values of another tables column where the primary key between is Part_Number?
table2 = temp_table 
Update Actuator_Components Set Scanned_Drawings = table2.Scanned_Drawings
 

View 2 Replies View Related

Update Table - Insert New Column

Jun 8, 2005

This should be easy for someone, but I just can't seem to find a sample to do this.....I have created a table...CREATE TABLE dbo.test   ( oId int NOT NULL UNIQUE,          test1 varchar(50) NOT NULL PRIMARY KEY                            )Now, I need to go back and simply add another column to the table such as       test2 varchar(50)Not sure if the insert is the way to go and been playing around with various statements but with no luck.Suggestions?Thanks all,Zath

View 2 Replies View Related

Alter Table New Column And Update

Nov 17, 2006

Hifor MS SQL 2000/2005I am having a table (an old database, not mine) with char value for the column [localisation]Users[name] [nvarchar] (100) NOT NULL ,[localisation] [nvarchar] (100)NULLNow i have created a table [Localisation]Localisation[id_Localisation] [int] NOT NULL,[localisation] [nvarchar] (100) NOT NULLI am adding a new column to UsersALTER TABLE [Users] ADD [id_Localisation] int NULLand I want to update the Column [Users].[id_Localisation] before to drop the column [Users].[Localisation]something like UPDATE [Users] SET id_Localisation = (SELECT Localisation.id_LocalisationFROM Localisation FULL OUTER JOINUsers ON Localisation.Localisation = Users.Localisation)Users.Localisation can have a NULL value (then no id_localisation return)but it doesnt work because it returns > 1 rowthank youhow can I do it ?

View 10 Replies View Related

Update Column Of A Table With A Sproc

Jun 10, 2008

I have a table with this structure

ID | Ticker
-------------------
1330 |AAB-Bank
1336 |AEGON
1367 |ALZSE
1420 |ASSGEN
2812 |AVLN

I have a sproc called usp_validTicker that will take 2 parameters: ticker and date. It will return the valid ticker for that date.
I like to have the sproc going through each ticker in the table and return the valid tickers.

For example
exec usp_validTicker 'AAB-Bank','2008-6-10' will return 'AAB' and my final table will be

ID | Ticker
-------------------
1330 |AAB
1336 |AEGON
1367 |ALZSE
1420 |ASSGEN
2812 |AVLN

View 13 Replies View Related

Trigger On Table Which Will Not Allow To Update Value 1 Into Column

Sep 25, 2014

I need to create trigger on table which will not allow to update value "1" into column and if tried to update.. then it should show error massage "Good To GO"

ID Name Roll
1 Ron 1
2 Jon 0
3 Nil 3
4 Par 1

if you try to update value "1" in Roll then it will through error

nil

View 4 Replies View Related

How To Update A Column With The Sum Of Two Columns In A Table

May 9, 2008



I have a simple question that is more about performance on my code for updating a column with the sum of two columns:

Example

Table = dbo.adding

c1 c2 c3Sum
1 2
3 5
4 6

Now if I just simple write

Update dbo.adding
set c3SUM = c1 + c2

This works, but there is a null in c1 or c2 then I know I can write this:


if exists (select * from dbo.adding where c1 is NULL)

Update dbo.adding

set c1 = '0', c3SUM = '0' + c2 where c1 is NULL

if exists (select * from dbo.adding where c2 is NULL)

Update dbo.adding

set c2 = '0', equals = c1 + '0' where c2 is NULL

Update dbo.adding

set c3SUM = c1 + c2

This works as well, but I am not sure how perficient it is being that from my understanding the code will evaluate the table three times and update it based on the criteria. If the table consists of 100,000 rows then this might not be a good solution

Is there a more proficient way to do this... I personally try to as little coding as possible to help increase performance. I know I can make this very complex and declare variables and put everything in a loop, but I personally think that would be less profient..

View 8 Replies View Related

Does The Actual Update Statement Of A Table Affect Merge Replication?

Dec 8, 2006

Suppose your using Merge Replication.

2 Users in 2 locations issue updates to the same table. 1 updating 1 column and the other updating another column. Now in reality the actual Stored Procedure issuing the update statement is passed in all the possible columns that could change and builds an update statement that updates all columns even the ones that havent changed.

Will this break Merge Replications conflict tracking? Or does SQL Server 2005 Merge Replication pickup that in reality the 2 updates only in reality changed the values in 2 columns.

View 1 Replies View Related

Stored Procedure To Update A Table Using Parameterized CASE Statement - Erroring Out

May 2, 2008

I am trying to create a stored procedure that will take a text value passed from an application and update a table using the corresponding integer value using a CASE statement. I get the error: Incorrect syntax near the keyword 'SET' when I execute the creation of the SP. What am I missing here? This looks to me like it should work. Here is my code.


CREATE PROCEDURE OfficeMove

-- Add the parameters for the stored procedure here

@UserName nvarchar(10),

@NewLocation nchar(5),

@NewCity nvarchar(250)

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

-- Insert statements for procedure here

Execute as user = '***'

DELETE FROM [SQLSZD].[SZDDB].dbo.Employee_Office_Assignments

WHERE User_Name = @UserName

INSERT INTO [SQLSZD].[SZDDB].dbo.Employee_Office_Assignments

SET User_Name = @UserName,

Room_ID = @NewLocation

UPDATE [SQLSZD].[SZDDB].dbo.Employee_Locations

SET Office_ID =

CASE

WHEN @NewCity = 'Columbus' THEN 1

WHEN @NewCity = 'Cleveland' THEN 2

WHEN @NewCity = 'Cincinnati' THEN 4

WHEN @NewCity = 'Raleigh' THEN 5

WHEN @NewCity = 'Carrollwood' THEN 6

WHEN @NewCity = 'Orlando' THEN 7

END

WHERE User_Name = @UserName

END

GO

View 4 Replies View Related

How To Update One Column To Be The Same For All Records In Database Table?

May 6, 2007

Hi,
I have a set of records in database table and I want to update one column to be the same for all of them.
Can you suggest code solution?

View 1 Replies View Related

Update Table And Views With A Hidden Column

Aug 30, 2000

My client want me to update a table and the view with a new column which should be hidden. I am kind of confused on how to go about this in order to get a good result. PLEASE HELP!!

View 1 Replies View Related

SQL Server 2012 :: How To Update One Column Value To Another In Same Table

Jun 21, 2014

my table payment_details structure is

payment_id payment_code
1 null
2 null
3 null
4 null

here payment_id is a primary key and i need to update the whole payment_id column to payment_code column.so i just tried the below query

update payment_details
set payment_code = payment_no
where payment_code is null

but it shows subquery error?

View 3 Replies View Related

SP With Inner Join - Update Table Value (Single Column)

Oct 16, 2015

I'm trying to do the following:

update a tables value(a single column), based on a query of another database, using a common column.

Here's what I've cooked up so far:

Declare @trackingNo nvarchar (50)
Set @trackingNo =
(
select tracking_no from P21_Shipping.dbo.shipping_data t1
inner join P21.dbo.oe_hdr t2 on t1.order_no = t2.order_no

[Code] ...

print @trackingNoThe error it's returning is:

Msg 512, Level 16, State 1, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

So, I'm wanting to query Shipping_data for order_No that matches the same orderNo column value in P21 Database(oe_hdr table), then update P21 oe_hdr table with "trackingNo from Shipping_data

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved