Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a variable value into server 2008 table [closed]

Using Sql Server 2008: I want to insert a specific value (for example: 3513CB65-7FF4-477D-B518-E7FF7B298CB) to a uniqueidentifier field of a table.

How can to declare the value and how to insert it?

like image 688
ZKF3340320 Avatar asked Sep 10 '26 05:09

ZKF3340320


1 Answers

You can use NEWID() function of TSQL as;

DECLARE @myUid uniqueidentifier
SET @myUid = NEWID()

INSERT INTO [MYTABLE] (UNIQUEIDENTIFIER_FIELD, SOME_OTHER_FIELD)
VALUES (@myUid, 'some other field data')

Or you could even use NEWID() directly in INSERT statement as:

INSERT INTO [MYTABLE] (UNIQUEIDENTIFIER_FIELD, SOME_OTHER_FIELD)
VALUES (NEWID(), 'some other field data')

Edit: For hardcoded value, you need to use single quotes (') as;

INSERT INTO [MYTABLE] (UNIQUEIDENTIFIER_FIELD, SOME_OTHER_FIELD)
VALUES ('3513CB65-7FF4-477D-B518-E7FF7B298CB2', 'some other field data')
like image 126
Saro Taşciyan Avatar answered Sep 12 '26 19:09

Saro Taşciyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!