Saturday, February 25, 2012

Newbie - Program

This is my first foray into MS SQL Server, so I am trying to get my feet wet
with a couple of things. One thing I am trying to do right now is fill in a
uniqueidentifier field I added to a table using the NEWID() function. In MS
Access, I would have cranked out a little VBA code that simply went through
all the records in the table one by one and setting the Record_ID (type
uniqueidentifier) field using NEWID().
Unfortunately, I don't have a warm and fuzzy on how to accomplish this
within SQL Server itself. Best I have been able to tell so far, I probably
want to be developing a procedure. But I am not sure how to accomplish what
I want to do using SQL.
Suggestions and pointers to references will be greatly appreciated!
Thanks!
DonHello, Don
You only need:
UPDATE YourTable SET YourColumn=NEWID()
That's because the NEWID() function will be called for each row
(therefore you will get a different value in each row).
Razvan|||In SQL server there is a thing called default value
You will see it in Enterprise Manager when you click on design table
In the default value property of a field you can type newid() and whenever a
new record is inserted it will 'automatically' get this value
Default values are very handy for Created Date you would give it a default
value of getdate()
Hope this helped you and did not add to the confusion
http://sqlservercode.blogspot.com/
"Don" wrote:

> This is my first foray into MS SQL Server, so I am trying to get my feet w
et
> with a couple of things. One thing I am trying to do right now is fill in
a
> uniqueidentifier field I added to a table using the NEWID() function. In
MS
> Access, I would have cranked out a little VBA code that simply went throug
h
> all the records in the table one by one and setting the Record_ID (type
> uniqueidentifier) field using NEWID().
> Unfortunately, I don't have a warm and fuzzy on how to accomplish this
> within SQL Server itself. Best I have been able to tell so far, I probabl
y
> want to be developing a procedure. But I am not sure how to accomplish wh
at
> I want to do using SQL.
> Suggestions and pointers to references will be greatly appreciated!
> Thanks!
> Don
>
>
>|||SQL,
While I was playing around, I discovered the default value and that if you
select that property when you create the field it will automatically fill in
values! In my first attempt, I had created the field, but not set the
default value so I had a table full of <Null> for that field (never mind the
issue of adding a new record later!).
Thanks for the help!!
Don
"SQL" <SQL@.discussions.microsoft.com> wrote in message
news:75975E43-A123-43DC-96C3-B120D0B35BF0@.microsoft.com...
> In SQL server there is a thing called default value
> You will see it in Enterprise Manager when you click on design table
> In the default value property of a field you can type newid() and whenever
> a
> new record is inserted it will 'automatically' get this value
> Default values are very handy for Created Date you would give it a default
> value of getdate()
> Hope this helped you and did not add to the confusion
> http://sqlservercode.blogspot.com/
>
> "Don" wrote:
>|||Razvan,
I actually solved my immediate problem a little differently, but your
suggestion will definitely help in the future!!
Thanks!
Don
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1127663811.868041.231450@.g43g2000cwa.googlegroups.com...
> Hello, Don
> You only need:
> UPDATE YourTable SET YourColumn=NEWID()
> That's because the NEWID() function will be called for each row
> (therefore you will get a different value in each row).
> Razvan
>|||Your entire mental model of SQL and RDBMS is totally wrong and will
take at least one year of solid work to change. This is based on 20+
years with SQL and teaching it.
Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files; there is no sequential access or
ordering in an RDBMS, so "first", "next" and "last" are totally
meaningless. NEWID and uniqueidentifier "field" are proprietary,
non-relational crap. Since you started with ACCESS, you need to add an
extra six months of UN-learning.
A newsgroup is a piss poor substitue for an education. Please read
something before you post again.

No comments:

Post a Comment