Solved

How can I  set a GUID column to ROWGUID using T-SQL?

Posted on 2008-06-24
6
1,152 Views
Last Modified: 2010-04-21
I have a source table with a primary key column that is a guid and is set as rowguid when the table was created.  I want to duplicate this table using a select-into command and then perform some actions on it and then drop the original table and rename this clone to the name of the original and continue.  I can use T-SQL to add the indexes and defaults back to the table but I can't figure out the syntax to set the column back to ROWGUID.  Does anyone know how to do it?
0
Comment
Question by:YellowbusTeam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
Chrisedebo earned 500 total points
ID: 21855055
This should do what you're after :o)
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.tablename ALTER COLUMN columnname
	ADD ROWGUIDCOL
GO
COMMIT

Open in new window

0
 

Author Closing Comment

by:YellowbusTeam
ID: 31470113
That's perfect, thanks Chrisedebo :)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21855082
like this:

ALTER TABLE TableName ALTER COLUMN guidfield
      ADD ROWGUIDCOL
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 7

Expert Comment

by:Chrisedebo
ID: 21855103
If you need to know the syntax for something you know how to do in sql management studio, make the change you want to then click on the generate change script button (looks like a little disk with a script behind it) on the tool bar and it will generate something similar to the script above.......not that I'm going to admit I'm a dirty cheat ;o)
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21855127
I'm the same way.  :)  I added the rowguid in management studio and watched profiler to see what the interface did.
0
 

Author Comment

by:YellowbusTeam
ID: 21863914
Aha - good tip -  I knew about that for creating things but didn't know you could find change syntax with it :)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question