Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › (SQL) Need a script to insert a new generated uniqueID value into a table.
New Posts  All Forums:Forum Nav:

(SQL) Need a script to insert a new generated uniqueID value into a table.

post #1 of 6
Thread Starter 
Hey OCNers,

I'm need a way to take a UniqueID value generated from this query:
Code:
Use ExampleDB

Go

Select NEWID() from tblName

Which generates a unique value like this:



I normally copy and past this value into the column where it goes, but I would rather create a script to do so for me. The problem is that it does not insert the generated value into a table but rather a "results" window, so I don't know where to pull the data from to insert it.

Any help would be appreciated
My System
(17 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X4 955  Gigabyte GA-MA790XT-UD4P Sapphire HD 6950 Sapphire HD 6950 
RAMHard DriveHard DriveOptical Drive
G-Skill Mushkin Callisto Deluxe WD Black LG Supermulti 
CoolingOSKeyboardPower
Rasa Black CPU water block. Rasa X20 pump/res c... Windows 7 Ultimate x64 Deck Legend Fire mechanical with Cherry Black s... Cooler Master Silent Pro Gold 1200W modular PSU. 
CaseMouseAudio
Cooler Master Storm Scout Logitech MX-518 Logitech Wireless headset for gaming and 100w M... 
  hide details  
Reply
My System
(17 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X4 955  Gigabyte GA-MA790XT-UD4P Sapphire HD 6950 Sapphire HD 6950 
RAMHard DriveHard DriveOptical Drive
G-Skill Mushkin Callisto Deluxe WD Black LG Supermulti 
CoolingOSKeyboardPower
Rasa Black CPU water block. Rasa X20 pump/res c... Windows 7 Ultimate x64 Deck Legend Fire mechanical with Cherry Black s... Cooler Master Silent Pro Gold 1200W modular PSU. 
CaseMouseAudio
Cooler Master Storm Scout Logitech MX-518 Logitech Wireless headset for gaming and 100w M... 
  hide details  
Reply
post #2 of 6
What?

Just use the NewID() like any other column value

Insert into tblName (ColumnName, value) values (NewID(), 'STUFF');
Tractorbox II
(6 items)
 
al dente
(11 items)
 
 
CPUMotherboardGraphicsRAM
Intel 4770k Gigabyte Z87-DS3H Nvidia Geforce 970 G.Skill DDR3-10700 
Hard DriveHard Drive
Samsung 840 Samsung 840 
CPUMotherboardRAMHard Drive
AMD Athlon 2 X2 240 ASRock 760GM-GS3 Kingston HyperX Blu Red Series 250Gig Seagate 
CoolingOSMonitor
Stock FX-6100 cooler Windows XP Wells Gardner 25K7194 
  hide details  
Reply
Tractorbox II
(6 items)
 
al dente
(11 items)
 
 
CPUMotherboardGraphicsRAM
Intel 4770k Gigabyte Z87-DS3H Nvidia Geforce 970 G.Skill DDR3-10700 
Hard DriveHard Drive
Samsung 840 Samsung 840 
CPUMotherboardRAMHard Drive
AMD Athlon 2 X2 240 ASRock 760GM-GS3 Kingston HyperX Blu Red Series 250Gig Seagate 
CoolingOSMonitor
Stock FX-6100 cooler Windows XP Wells Gardner 25K7194 
  hide details  
Reply
post #3 of 6
I think this should help a bit : http://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx


EDIT: Wait, it doesn't add anything, it just show your new Guid.
To insert data, refer to previous answer.
post #4 of 6
Quote:
Originally Posted by PhillyOverclocker View Post

Hey OCNers,

I'm need a way to take a UniqueID value generated from this query:
Code:
Use ExampleDB

Go

Select NEWID() from tblName

Which generates a unique value like this:



I normally copy and past this value into the column where it goes, but I would rather create a script to do so for me. The problem is that it does not insert the generated value into a table but rather a "results" window, so I don't know where to pull the data from to insert it.

Any help would be appreciated

Why would you use a script when you can just use SQL? smile.gif

Did you want this value to be auto-generated on insert of a new record? If so, you can define the DEFAULT value of the field.
http://technet.microsoft.com/en-us/library/ms190348.aspx


If you want to create the value for existing records, then do what kevmatic said.... NEWID() is just like any other value.

To be a bit more clear:
Insert into tblName (ColumnName1, ColumnName2) values (NewID(), 'STUFF');
Edited by DuckieHo - 9/23/13 at 9:46am
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #5 of 6
Thread Starter 
Boy was I really overthinking that one. Thanks a lot for the help.

I have been doing some light scripting trying to streamline some of the more redundant tasks for when we ship out servers to new customers. The problem is, the guy who had the position before me was worse with SQL than I am so he really didn't leave much behind in the ways of useful scripts to learn from. I guess he just did everything manually which is pretty ridiculous considering the amount of hours time it takes to do it all by hand, opposed to learning how to write a few basic scripts and do it all in a few minutes.

As basic as it is, here is my final solution;
Code:
Use ExampleDB
Go
update tblTableName
set ColumnName = NewID()

That's it. Worked like a charm. This is going to be part of a much larger script that does many tasks btw.

Thanks again, you guys might be seeing me back from time to time as I get stuck on things.

PS: Good to see you are still around Duckie. I've been coming back to OCN slowly now that I have a new comfy position with more free time. smile.gif
My System
(17 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X4 955  Gigabyte GA-MA790XT-UD4P Sapphire HD 6950 Sapphire HD 6950 
RAMHard DriveHard DriveOptical Drive
G-Skill Mushkin Callisto Deluxe WD Black LG Supermulti 
CoolingOSKeyboardPower
Rasa Black CPU water block. Rasa X20 pump/res c... Windows 7 Ultimate x64 Deck Legend Fire mechanical with Cherry Black s... Cooler Master Silent Pro Gold 1200W modular PSU. 
CaseMouseAudio
Cooler Master Storm Scout Logitech MX-518 Logitech Wireless headset for gaming and 100w M... 
  hide details  
Reply
My System
(17 items)
 
  
CPUMotherboardGraphicsGraphics
AMD Phenom II X4 955  Gigabyte GA-MA790XT-UD4P Sapphire HD 6950 Sapphire HD 6950 
RAMHard DriveHard DriveOptical Drive
G-Skill Mushkin Callisto Deluxe WD Black LG Supermulti 
CoolingOSKeyboardPower
Rasa Black CPU water block. Rasa X20 pump/res c... Windows 7 Ultimate x64 Deck Legend Fire mechanical with Cherry Black s... Cooler Master Silent Pro Gold 1200W modular PSU. 
CaseMouseAudio
Cooler Master Storm Scout Logitech MX-518 Logitech Wireless headset for gaming and 100w M... 
  hide details  
Reply
post #6 of 6
Quote:
Originally Posted by PhillyOverclocker View Post

Boy was I really overthinking that one. Thanks a lot for the help.

I have been doing some light scripting trying to streamline some of the more redundant tasks for when we ship out servers to new customers. The problem is, the guy who had the position before me was worse with SQL than I am so he really didn't leave much behind in the ways of useful scripts to learn from. I guess he just did everything manually which is pretty ridiculous considering the amount of hours time it takes to do it all by hand, opposed to learning how to write a few basic scripts and do it all in a few minutes.

As basic as it is, here is my final solution;
Code:
Use ExampleDB
Go
update tblTableName
set ColumnName = NewID()

That's it. Worked like a charm. This is going to be part of a much larger script that does many tasks btw.

Thanks again, you guys might be seeing me back from time to time as I get stuck on things.

PS: Good to see you are still around Duckie. I've been coming back to OCN slowly now that I have a new comfy position with more free time. smile.gif

If you always want ColumnName to be NEWID(), you can also just define it as such:


create table tblTableName (
ColumnName uniqueidentifier NOT NULL DEFAULT newid()
)
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › (SQL) Need a script to insert a new generated uniqueID value into a table.