June 25, 2007 at 7:52 am
Hello all,
The following update statement in a script:
UPDATE
#TempRecordsToCreate
SET
#TempRecordsToCreate.ShortName = CODE_PROGRAM.CODE_VALUE
FROM
CODE_PROGRAM
WHERE
#TempRecordsToCreate.ShortName = CODE_PROGRAM.MEDIUM_DESC
I am trying to update ShortName in a temp table with CODE_VALUE from a database table where ShortName = MEIDUM_DESC. It does what I expect as long as there are no spaces in the ShortName string. As an example ShortName 'Amman' becomes CODEVAUE 'AMMAN' as expected but 'Paris CFS' does not become 'PARCSF'.
I have tried rewriting the UPDATE to use a subselect:
UPDATE
#TempRecordsToCreate
SET
#TempRecordsToCreate.ShortName = (SELECT a.CODE_VALUE FROM CODE_PROGRAM a JOIN #TempRecordsToCreate b ON a.MEDIUM_DESC = b.ShortName)
This returns NULL in a case like 'Paris CFS'. I have tried timming, replacing the internal spaces, casting to identical datatypes etc.
Nothing works!
Why cant' SQL Server see 'Paris CFS' = 'Paris CFS' ???
Thnaks!
Jonathan
June 25, 2007 at 8:24 am
Try
UPDATE
t
SET t.ShortName = w.CODE_VALUE
FROM #TempRecordsToCreate AS t
INNER JOIN CODE_PROGRAM AS w ON w.MEDIUM_DESC = t.ShortName
If it doesn't work, please post DDL for the two tables.
N 56°04'39.16"
E 12°55'05.25"
June 25, 2007 at 8:35 am
Thanks Peter,
That does not work either! Here is the DDL
CREATE
TABLE #TempRecordsToCreate
(
ApplicantID
int,
FirstName
nvarchar(255),
LastName
nvarchar(255),
nvarchar(255),
Term
nvarchar(50),
[Year]
int,
ShortName
nvarchar(100)
)
CREATE
TABLE [dbo].[CODE_PROGRAM](
[CODE_VALUE_KEY] [varchar]
(6) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CODE_VALUE] [varchar]
(6) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SHORT_DESC] [varchar]
(10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[MEDIUM_DESC] [varchar]
(20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[LONG_DESC] [varchar]
(40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[STATUS] [varchar]
(1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CREATE_DATE] [datetime]
NOT NULL,
[CREATE_TIME] [datetime]
NOT NULL,
[CREATE_OPID] [varchar]
(8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CREATE_TERMINAL] [varchar]
(4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[REVISION_DATE] [datetime]
NOT NULL,
[REVISION_TIME] [datetime]
NOT NULL,
[REVISION_OPID] [varchar]
(8) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[REVISION_TERMINAL] [varchar]
(4) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CODE_XVAL] [varchar]
(10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[CODE_XDESC] [varchar]
(64) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ABT_JOIN] [varchar]
(1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[FTE_CREDITS] [numeric]
(6, 2) NOT NULL,
[PROGRAM_TYPE] [varchar]
(1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [C_PROGRAM_PK] PRIMARY KEY CLUSTERED
(
[CODE_VALUE_KEY]
ASC
)
ON [PRIMARY]
)
ON [PRIMARY]
June 25, 2007 at 9:11 am
One reason might be that you are JOINING on varchar and nvarchar?
Which collation do the temporary table use?
N 56°04'39.16"
E 12°55'05.25"
June 25, 2007 at 2:28 pm
Both the temp db and the db of CODE_PROGRAM table have collation = SQL_Latin1_General_CP1_CI_AS.
I have also tried creating the temp table with a SELECT INTO so that I know the collations are the same.
I have tried to deal with the differing datatypes with:
UPDATE #TempRecordsToCreate
SET #TempRecordsToCreate.ShortName = CODE_PROGRAM.CODE_VALUE
FROM CODE_PROGRAM
WHERE #TempRecordsToCreate.ShortName = CAST(CODE_PROGRAM.MEDIUM_DESC AS nvarchar(100))
Nothing works!
Help!!!!
Jonathan
June 25, 2007 at 2:35 pm
The SET statement does not allow the following...
SET #TempRecordsToCreate.ShortName = CODE_PROGRAM.CODE_VALUE
Remove the code in red and it should work fine...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 25, 2007 at 2:37 pm
Try
SELECT ShortName , CONVERT(varbinary(200), ShortName)
FROM #TempRecordsToCreate
SELECT MEDIUM_DESC, CONVERT(varbinary(200), MEDIUM_DESC)
FROM CODE_PROGRAM
See if there is a difference not visible by eyes.
_____________
Code for TallyGenerator
June 25, 2007 at 2:39 pm
I am pretty sure your column value has PADDING Spaces.
Try selecting DATALENGTH on your column
* Noel
June 25, 2007 at 2:41 pm
Post some sample data for both tables. Also provide expected output.
N 56°04'39.16"
E 12°55'05.25"
June 25, 2007 at 3:00 pm
I have found my problem!
Unknown to me a group of MEDIUM_DESC were removed from the CODE_PROGRAM table this morning. They happened to mostly be MEDIUM_DESC with internal spaces and I jumped to a false conclusion!
Thank you all for your help.
Good old SQL Server is doing exactly what it is asked to do once again.
Jonathan
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply