[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

sql code question

Posted on 2013-01-09
8
Medium Priority
?
284 Views
Last Modified: 2013-01-09
In this code, will the print just display the contents of @cmd or will the @cmd be executed?

declare @table nvarchar(128)
declare @cmd nvarchar(500)
declare AllTables cursor for
select name from sys.tables
   where create_date >='2013-01-08'
open AllTables
fetch next from AllTables into @table
while @@fetch_status = 0
begin
  set @cmd = N'exec sp_rename ' + @table + ',' + 'XLElib.' + @table
  print @cmd
  fetch next from AllTables into @table
end
0
Comment
Question by:qbjgqbjg
  • 4
  • 4
8 Comments
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38759764
to execute you need

EXEC (@cmd)

for print
PRINT @cmd
0
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38759779
instead of cursor why dont you try this

--CREATE SCHEMA TestSchema
--USE Test01
--GO

DECLARE @SQL VARCHAR(MAX)
DECLARE @Change VARCHAR(MAX)

set @Change = 'ALTER SCHEMA TestSchema TRANSFER dbo.['

SELECT
	 @SQL = COALESCE(@SQL + @Change + [name] + '] ', @Change + [name] + '] ')
FROM
	sys.tables
--WHERE
	--create_date = Your condition

PRINT @SQL

EXEC (@SQL)  

Open in new window

0
 

Author Comment

by:qbjgqbjg
ID: 38759834
I don't quite understand what your code is doing?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38760198
do you want to change schema of table or just table name?
0
 

Author Comment

by:qbjgqbjg
ID: 38760211
Either will work.
0
 
LVL 5

Accepted Solution

by:
RehanYousaf earned 2000 total points
ID: 38760241
try this for schema change
DECLARE @SQL VARCHAR(MAX)
DECLARE @Change VARCHAR(MAX)

set @Change = 'ALTER SCHEMA TestSchema XLElib dbo.['

SELECT
	 @SQL = COALESCE(@SQL + @Change + [name] + ']; ', @Change + [name] + ']; ')
FROM
	sys.tables
WHERE
	create_date >= '2011-01-08'

PRINT @SQL

EXEC (@SQL)  

Open in new window

0
 

Author Comment

by:qbjgqbjg
ID: 38760258
Thanks, I'll try it.
0
 

Author Closing Comment

by:qbjgqbjg
ID: 38760462
It looks like this will work. Thanks
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview

872 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