Solved

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

Posted on 2008-06-24
6
1,140 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to union three related tables in one flat file? 8 48
Powershell v3 - SQLCMD 3 27
sql server query 18 40
MS SQL SERVER and ADODB.commands 8 23
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 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