Solved

SQL Query with quoted identifier

Posted on 2012-03-21
13
475 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:basile
  • 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:basile
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:basile
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:basile
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:basile
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:basile
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

816 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

8 Experts available now in Live!

Get 1:1 Help Now