| 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)" .ThanksL |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 SQLUPDATE 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? |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2009-11-18 : 11:45:06
|
| [code]Declare @records intSET @records = 0UPDATE dbo.person SET Last Name = 'Jones' WHERE Telephone Extension = '02874 012450'SET @records = @records + @@rowcountUPDATE dbo.person SET Last Name = 'Hamill' WHERE Telephone Extension = '02874 023450'SET @records = @records + @@rowcountUPDATE dbo.person SET Last Name = 'Sayeed' WHERE Telephone Extension = '02874 212450'SET @records = @records + @@rowcountprint @records[/code] |
 |
|
|
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 |
 |
|
|
aoriju
Posting Yak Master
156 Posts |
Posted - 2009-11-19 : 07:35:57
|
| You can Use Case structureJustLikeUpdate Table Set FieldName = CASE WHEN Telephone Extension = '02874 012450' THEN Jones' WHEN Telephone Extension = '02874 023450' THEN 'Hamill' ELSE 'TEST' END |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 egColumn A - add extensionsColum B - add all 20 desk locationsColumn C - Add: ".="UPDATE dbo.person SET Desk = '" & B1 & "' WHERE Extension = '" & A1 & "'""Then copy (drag) from C1 to C20I then copy the resulting code to Query Analyzer.Hope this is clear.L |
 |
|
|
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. |
 |
|
|
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 egColumn A - add extensionsColum B - add all 20 desk locationsColumn C - Add: ".="UPDATE dbo.person SET Desk = '" & B1 & "' WHERE Extension = '" & A1 & "'""Then copy (drag) from C1 to C20I 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 QAIf 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 tableCREATE TABLE #myPerson99 (LastName varchar(50), Phone varchar(50))GO-- Put Rows in to update, leave 1 out for the need to InsertINSERT INTO #myPerson99(LastName, Phone)SELECT 'Jonesy', '02874 012450' UNION ALLSELECT 'Marky Mark', '02874 023450'-- Show The data in the Table as it stand BEFORE DMLSELECT * FROM #myPerson99-- Example of how I would not use Excel and just create a table variableDECLARE @excel table (LastName varchar(50), Phone varchar(50))INSERT INTO @excel(LastName, Phone)SELECT 'Jones', '02874 012450' UNION ALLSELECT 'Hamill', '02874 023450' UNION ALLSELECT 'Sayeed', '02874 212450'-- Now just perform a Set update..inner join means existance UPDATE t SET LastName = e.LastName FROM #myPerson99 tINNER JOIN @excel e ON e.Phone = t.Phone-- INSERT uses non existenceINSERT INTO #myPerson99 (LastName, Phone) SELECT e.LastName, e.Phone FROM @excel eWHERE NOT EXISTS(SELECT * FROM #myPerson99 t WHERE e.Phone = t.Phone)-- Let's Take a lookSELECT * FROM #myPerson99GODROP TABLE #myPerson99GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
|