Solved

SQL Query with quoted identifier

Posted on 2012-03-21
13
483 Views
Last Modified: 2012-08-14
i'm trying to use the sp_estimate_data_compression_Savings

to get all tables;

i'm getting an error:

i want it to look like this:

EXEC sp_estimate_data_compression_savings 'dbo', 'fact_WebActivity', NULL, NULL, 'ROW' ;

Trying this: so that i get all tabels

select 'EXECUTE sp_estimate_data_compression_savings ''dbo''' + ',' + ''''+ name from sys.tables where type = 'u' + '''
0
Comment
Question by:Auerelio Vasquez
[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
  • 6
  • 5
  • 2
13 Comments
 
LVL 16

Expert Comment

by:DcpKing
ID: 37749441
You have to prefix every tick (  '  ) with another, and then wrap the thing in ticks!

declare @str varchar(1000)
set @str = 'sp_estimate_data_compression_savings '
set @str = @str + '''dbo'', '
set @str = @str + '''fact_WebActivity'' '
set @str = @str + ', NULL, NULL, ''ROW'' ;'
--print @str       --do this to check that it's right !
exec @str

I did it in four steps to show how more clearly.
0
 
LVL 6

Expert Comment

by:netjgrnaut
ID: 37749485
select 'EXEC sp_estimate_data_compression_savings ''dbo,''' + name  + ''',NULL,NULL,''ROW''' from sys.tables where type = 'u'

Open in new window

0
 
LVL 16

Expert Comment

by:DcpKing
ID: 37749497
(oops - missed parens around the @str - sorry!)
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 6

Expert Comment

by:netjgrnaut
ID: 37749516
Oh, I thought you were trying to build a literal set of command lines, to execute later (based on your SELECT statement shown).

The select statement I posted above produces the following output...

EXEC sp_estimate_data_compression_savings 'dbo,'r3_tracker-20110926_baseline',NULL,NULL,'ROW'
EXEC sp_estimate_data_compression_savings 'dbo,'srcBL_GroupMembers',NULL,NULL,'ROW'
EXEC sp_estimate_data_compression_savings 'dbo,'r3_tracker-20110926_rit3010422',NULL,NULL,'ROW'
EXEC sp_estimate_data_compression_savings 'dbo,'srcBL_Groups',NULL,NULL,'ROW'
EXEC sp_estimate_data_compression_savings 'dbo,'r3_tracker-20110927_rit301(2725-3311-3393)',NULL,NULL,'ROW'
EXEC sp_estimate_data_compression_savings 'dbo,'srcBL_InteractiveRights',NULL,NULL,'ROW'

Open in new window

0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 37749972
@netjgrnaut

You are correct in your assumption. That's what i wanted. The only issue:

I need the second tick mark around dbo like 'dbo'

to look like this

EXEC sp_estimate_data_compression_savings 'dbo','fact_WebActivity',1,NULL,'Page'
0
 
LVL 6

Expert Comment

by:netjgrnaut
ID: 37749977
Whoops. Put two ticks after dbo, before the period. That should do it.
0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 37752773
ok. so this worked .  i was overthinking it. One last question regarding this, how is the best way, to get each line returned from all of these exec statements, into one table ?

select 'EXEC sp_estimate_data_compression_savings ''dbo'',''' + name  + ''',NULL,NULL,''ROW''' from sys.tables where type = 'u'
0
 
LVL 6

Accepted Solution

by:
netjgrnaut earned 500 total points
ID: 37752787
select 'EXEC sp_estimate_data_compression_savings ''dbo'',''' + name  + ''',NULL,NULL,''ROW''' as SQLCMD into doCommands from sys.tables where type = 'u'

Open in new window


...will create a table named doCommands with a single column (SQLCMD) where each row is one command returned from the query.
0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 37753347
I apologize, what i meant, was the results from all of those queries, into one table, one row at a time. this give all the commans, which is how i asked it, but i wanted the results. these come out one execution at a time. is there a way, to that result, and insert into a table. i thought of a cursor for this, but i can't get my hands around all the tick marks, to put into a @String varable.....
0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 37753352
i can put this into another question, so i can award these points, if you like.
0
 
LVL 6

Expert Comment

by:netjgrnaut
ID: 37753380
A new topic would probably be best, to help those looking for specific answers in the future.
0
 
LVL 1

Author Comment

by:Auerelio Vasquez
ID: 37753780
Can I direct that to u somehow?
0
 
LVL 6

Expert Comment

by:netjgrnaut
ID: 37753795
I'll check your profile for open questions later this afternoon... but there may be better folks to provide you with a quick answer.

Thanks for the vote of confidence!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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