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
Solved

SQL Query with quoted identifier

Posted on 2012-03-21
13
479 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
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.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Script to Remove Data from Two Joined Tables 1 19
SQL Server / Update DB? 22 36
sql, case when & top 1 14 26
Error when creating a table from a function 6 17
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

840 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