SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


update statement where SET value is variable


update statement where SET value is variable

Author
Message
scott_lotus
scott_lotus
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 395
PROBLEM: Table J.JTypeID is full of NULLs.

I would like to run a statement to update J.JTypeID with the relevant Jtype.JTypeID per Jtype.CID where IsDefaultDeviceJState is the row selected per CID.

-----------------------------------------------------
CREATE TABLE [dbo].[J](
[JID] [int] IDENTITY(1,1)
[JTypeID] [int]
[DID] [int]

CREATE TABLE [dbo].[JType](
[JTypeID] [int] IDENTITY(1,1)
[CID] [int]
[IsDefaultDeviceJState] [bit]

CREATE TABLE [dbo].[D](
[CID] [int]
[DID] [int] IDENTITY(1,1)
-----------------------------------------------------
Sample data:

tbl.J
[JID] 1,2,3
[JTypeID] NULL,NULL,NULL
[DID] 1,2,1

tbl.JType
[JTypeID] ,1,2,3,4,5,6
[CID] 1,1,1,2,2,2
[IsDefaultDeviceJState] 1,0,0,0,0,0,1

tbl.D
[CID] 1,2
[DID] 1,2
-----------------------------------------------------

Basics probably but I keep getting in a muddle on my test data. Thank you for any help.
Scott
anthony.green
anthony.green
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10158 Visits: 6378

CREATE TABLE [dbo].[J]([JID] [int],[JTypeID] [int],[DID] [int])
CREATE TABLE [dbo].[JType]([JTypeID] [int],[CID] [int],[IsDefaultDeviceJState] [bit])
CREATE TABLE [dbo].[D]([CID] [int],[DID] [int])

INSERT INTO J VALUES (1,NULL,1),(2,NULL,2),(3,NULL,1)
INSERT INTO JType VALUES (1,1,1),(2,1,0),(3,1,0),(4,2,0),(5,2,0),(6,2,1)
INSERT INTO D VALUES (1,1),(2,2)

UPDATE J
SET JTypeID = JType.JTypeID
FROM
J
INNER JOIN
D
ON
J.DID = D.DID
INNER JOIN
JType
ON
D.CID = JType.CID

SELECT * FROM J





Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
When a question, really isn't a question - Jeff Smith
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


scott_lotus
scott_lotus
SSC-Enthusiastic
SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)SSC-Enthusiastic (120 reputation)

Group: General Forum Members
Points: 120 Visits: 395
Thank you for the reply and the reformatting.

I was close then with this attempt before posting :-)

update J
set J.JTypeID = Jtype.JTypeID
from jtype
inner join J on J.DID = D.DID
inner join D on D.CID = JType.CID

It was the SET line that was wrong but dont understand why exactly.

Tagged on "where JType.IsDefaultDeviceJState = 1" onto the end and we have a winner.

Thank you very much , great help.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search