Solved

sql code question

Posted on 2013-01-09
8
267 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
 
LVL 5

Expert Comment

by:RehanYousaf
ID: 38760198
do you want to change schema of table or just table name?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now