SQL Server - update table question

Asked By EddWood
18-Nov-09 01:03 PM
MSSQL 2005

I have this script that updates items in a table A if the codeID are equal
in table B. However, I also need to add any records to table A if there is a
record in table B that does not currently exists in table A, can anyone
advise how I can modify this or add a new script that will update/insert any
new records

thanks
Ed

script>>

UPDATE d
SET d.[T1_Code] = p.[Code],
d.[T1_productname]= p.[Product Name],
d.[T1_rrp] = p.[Unit RRP],
d.[T1_VAT] = p.[VAT Rate],
d.[T1_barcode] = p.[Barcode],
FROM [DS_T1] d
RIGHT JOIN [T2] p
ON d.[T1_code]= p.[Code];
SQL Server
(1)
CodeID
(1)
Productname
(1)
Nospam
(1)
Rrp
(1)
  Erland Sommarskog replied to EddWood
18-Nov-09 06:51 PM
EddWood (Edd_nospam_@/Wood\hotmail.co.uk) writes:

RIGHT JOIN should probably only be JOIN. I am not even sure that I under-
stand what the semantics for RIGHT JOIN would be here.

The INSERT statement you need would be:

INSERT DS_T1(T1_Code, T1_productname, T1_rrp, T1_VAT, T1_barcode)
SELECT p.Code, p.[Product Name], p.[Unit RRP], p.[VAT Rate], p.Barcode
FROM   T2 p
WHERE  NOT EXISTS (SELECT *
FROM   DS_T1 d
WHERE  d.T1_Code = p.Code)


I am only using brackets where they are required for better legibility.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
  EddWood replied to Erland Sommarskog
19-Nov-09 04:32 AM
Perfect, thank you
Create New Account
help
How to Create a Parameterised GROUP BY Without Using Dynamic-SQL SQL Server Hi all I have read Erland's treatise on dynamic-SQL, and now I want to avoid it like the plague. But one of the areas to group on different columns. What is the neatest way to do this without dynamic-SQL, if there is one, or the dirtiest if not? TIA Charles SQL Server Programming Discussions SQL Server 2008 (1) Grouping (1) Describe (1) LINQ (1) Database (1) CodeID (1) SteveThanks (1
using ROW_NUMBER to count SQL Server I have the query below that uses ROW_NUMBER to create count columns. The first 2 columns I create a count using codeID, which will never have a NULL value. However, the 3rd column, refID, can have NULLS does not count rows for refID that contains NULLs. So far I have this: SELECT codeID, codeTitle, codeText, refID, 'DR' + CAST(ROW_NUMBER() OVER(ORDER BY codeID) AS VARCHAR) AS DRid, 'VR' + CAST(ROW_NUMBER() OVER(ORDER BY codeID) AS VARCHAR) AS VRid, CASE WHEN refID IS NOT NULL THEN 'Ref' + CAST(ROW_NUMBER() OVER END FROM codeList If anyone could help me out I would greatly appreciate it. Thanks SQL Server Programming Discussions SQL Server 2005 (1) SQL Server 2000 (1) SQL Server (1) PRIMARY KEY
Rendering Rich texts in reports SQL Server HI guys, I was able to render richtext in SQL Server reporting Services 2005 by converting richtext into an image using this as reference: http: / / binaryworld.net / Main / CodeDetail.aspx?CodeId = 4049&atlanta = software%20development Everything seems to be fine using the preview feature in BI, however manager the rich text images were not rendered. What am I missing here? Cheers, Poggs SQL Server Reporting Services Discussions SQL Server (1) RTFConverter (1) PublicKeyToken (1) Report (1) FullTrust (1) CodeId (1) Mscorlib (1) Richtext
Transaction Abort in a trigger SQL 2008, writing to event log or t SQL Server I have seen tons of things out there telling oyu to rollback a transaction in ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [Common].[Code]( [CodeID] [int] NOT NULL IDENTITY(1, 1), [Code] [varchar](128) NOT NULL, [Category] [varchar](128) NOT NOT NULL, [UpdatedOn] [datetime] NOT NULL, [Version] [timestamp] NOT NULL, CONSTRAINT [pk_ _Code_ _CodeID] PRIMARY KEY CLUSTERED ( [CodeID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [COR_Common] ) ON Dropping and recreating procedure ''%s''', 0, 1, 'Common.upErrorHandler') drop procedure Common.upErrorHandler end go / * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = SQL Server Programming Discussions SQL Server (1) Oracle (1) Common.upErrorHandler (1) Stored procedure (1) CREATE PROCEDURE (1) DROP PROCEDURE
try. . .catch execution issue SQL Server According to BOL unless you physically return in the catch, any code after the "end not execute any code or set the return code of the procedure. This is in SQL Server 2008. There are 5 tables and 5 prodcedures in all. If you email me, I of the DB to see what I am referrring to. Has anyone else expereienced this? SQL Server Programming Discussions SQL Server 2008 R2 (1) SQL Server 2005 (1) SQL Server 2000 (1) SQL Server 2008 (1