Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get primary key for all table

Author  Topic 

SadafKhan85
Starting Member

8 Posts

Posted - 2013-07-03 : 07:41:56
In order to take automated backup of all user databases below is the query.
This query will eliminate use of manual backups for user databases, in order to fully automate this just create a SQL Agent job and write this query in the job and forget about taking any manual DB backups.

DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'C:\DB_BKPUP\'

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName

FETCH NEXT FROM db_cursor INTO @name
END

CLOSE db_cursor
DEALLOCATE db_cursor

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2013-07-03 : 08:13:06
Ummm.... this is a backup script, its not returning primary keys for any tables.


Duane.
http://ditchiecubeblog.wordpress.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-03 : 08:22:12
Sorry code posted has no relationship with post title. Title asks for PK columns and code posted speaks about DB backups and there's no question either!

Anyways answer to your question is to look for information_schema.table_constraints

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-07-03 : 08:41:28
Cut & Paste error. Duplicate of http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=186579


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -