Solved

sql code question

Posted on 2013-01-09
8
277 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
[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
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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