Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple Updates

Author  Topic 

Lozt
Starting Member

4 Posts

Posted - 2009-11-18 : 08:40:45
Hi,

I am updating multiple fields, via formating into single rows within Excel.

For example:

UPDATE dbo.person SET Last Name = 'Jones' WHERE Telephone Extension = '02874 012450' >> (1 row(s) affected)
UPDATE dbo.person SET Last Name = 'Hamill' WHERE Telephone Extension = '02874 023450'>> (1 row(s) affected)
UPDATE dbo.person SET Last Name = 'Sayeed' WHERE Telephone Extension = '02874 212450'>> (0 row(s) affected)

Can you tell me:

(a) Is there an easier way of doing this?
(b) Is there an automatic way to confirm the number of extensions didn't update or didn't exist e.g. "(0 row(s) affected)" .

Thanks
L

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-18 : 09:57:45
Easier?

Well that's open for interpretation.

But you could insert the values to a table and then do an update join



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Lozt
Starting Member

4 Posts

Posted - 2009-11-18 : 11:38:38
Hi Brett,

Thanks for the reply.

I'd normally use the following formula in Excel:

".="UPDATE dbo.person SET Last Name = '" & C2 & "' WHERE TelephoneExtension = '" & B2 & "'""

& copy the following to SQL
UPDATE dbo.person SET Last Name = 'Jones' WHERE Telephone Extension = '02874 012450'

If I'm updating 200 + rows, is this the quickest and simplest route?

If 10 of the 200 extensions did not appear in the database, how would I include additional T-SQL commands to alert me that these extensions need to be added to the database?



Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-18 : 11:45:06
[code]
Declare @records int
SET @records = 0
UPDATE dbo.person SET Last Name = 'Jones' WHERE Telephone Extension = '02874 012450'
SET @records = @records + @@rowcount
UPDATE dbo.person SET Last Name = 'Hamill' WHERE Telephone Extension = '02874 023450'
SET @records = @records + @@rowcount
UPDATE dbo.person SET Last Name = 'Sayeed' WHERE Telephone Extension = '02874 212450'
SET @records = @records + @@rowcount

print @records[/code]
Go to Top of Page

Lozt
Starting Member

4 Posts

Posted - 2009-11-19 : 03:53:14
Hi Russell,

Re. the count, it works great, thanks for the update

However, in my original post 02874 212450 doesn't exist on the database. As it would be quite painful searching 200 rows to find this extension, is there quick way of identifying it?

Cheers,
L
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2009-11-19 : 07:35:57
You can Use Case structure
JustLike
Update Table Set FieldName = CASE WHEN Telephone Extension = '02874 012450' THEN Jones'
WHEN Telephone Extension = '02874 023450' THEN 'Hamill'
ELSE 'TEST' END

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 09:36:34
Wait,

I don't get it

You are using Excel as a user interface to SQL Server?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

Lozt
Starting Member

4 Posts

Posted - 2009-11-19 : 11:13:19
Hi Brett,

No.

An example - I have 20 employees who move desk so, instead of altering & entering code 1 by 1, I use formulas in excel eg

Column A - add extensions
Colum B - add all 20 desk locations
Column C - Add: ".="UPDATE dbo.person SET Desk = '" & B1 & "' WHERE Extension = '" & A1 & "'""

Then copy (drag) from C1 to C20

I then copy the resulting code to Query Analyzer.

Hope this is clear.

L
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-19 : 11:15:53
I build queries that way all the time when folks request updates and send me a spreadsheet.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-19 : 13:28:35
quote:
Originally posted by Lozt

Hi Brett,

No.

An example - I have 20 employees who move desk so, instead of altering & entering code 1 by 1, I use formulas in excel eg

Column A - add extensions
Colum B - add all 20 desk locations
Column C - Add: ".="UPDATE dbo.person SET Desk = '" & B1 & "' WHERE Extension = '" & A1 & "'""

Then copy (drag) from C1 to C20

I then copy the resulting code to Query Analyzer.

Hope this is clear.

L




OH, you are GENERATING THE DML!!!!



I would just do it directly in QA

If I have to give it to a Business Person, I've written Access app's where they eneter the info, and then I generate the DML from that

...in any case..."I see said the blind man"

So......."Back where I come from...."

I'd do this



-- Example of your table
CREATE TABLE #myPerson99 (LastName varchar(50), Phone varchar(50))
GO

-- Put Rows in to update, leave 1 out for the need to Insert
INSERT INTO #myPerson99(LastName, Phone)
SELECT 'Jonesy', '02874 012450' UNION ALL
SELECT 'Marky Mark', '02874 023450'

-- Show The data in the Table as it stand BEFORE DML

SELECT * FROM #myPerson99

-- Example of how I would not use Excel and just create a table variable

DECLARE @excel table (LastName varchar(50), Phone varchar(50))
INSERT INTO @excel(LastName, Phone)
SELECT 'Jones', '02874 012450' UNION ALL
SELECT 'Hamill', '02874 023450' UNION ALL
SELECT 'Sayeed', '02874 212450'

-- Now just perform a Set update..inner join means existance

UPDATE t
SET LastName = e.LastName
FROM #myPerson99 t
INNER JOIN @excel e
ON e.Phone = t.Phone

-- INSERT uses non existence

INSERT INTO #myPerson99 (LastName, Phone)
SELECT e.LastName, e.Phone
FROM @excel e
WHERE NOT EXISTS(SELECT * FROM #myPerson99 t
WHERE e.Phone = t.Phone)

-- Let's Take a look

SELECT * FROM #myPerson99
GO

DROP TABLE #myPerson99
GO





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -