?
Solved

T-SQL help... short script

Posted on 2013-05-12
10
Medium Priority
?
247 Views
Last Modified: 2013-05-13
As part of a stored procedure I have:

select @sql = '
INSERT INTO ' + @tblTarget + '
SELECT  COUNT(DISTINCT ' +REPLACE(@where,',','),COUNT(DISTINCT ') + ')--,0
FROM ' + @tblTarget
exec(@sql)

This counts the number of entries and puts in the table @tblTarget like:

tblTarget
============
field1
--------
3     <-- The number of entries are 3 counting A, B and C)
A
B
C

but, I want to have it like:
tblTarget
============
field1
--------
<3>
A
B
C

I know that I have to conncatinate '<' and '>' at either end and possibly use CAST to change the count() as varchar(3) for example.

After a couple of tries, I see that I need some help in the syntax.

Question, How can I change the counts to read like <3> instead of just 3.

Thank you.
0
Comment
Question by:Mike Eghtebas
  • 5
  • 4
10 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39159840
try this

select @sql = 'INSERT INTO ' + @tblTarget 
+ 'SELECT  ' + '''' + '<' + '''' + ' + ' + ' CAST(COUNT(DISTINCT ' +REPLACE(@where,',','),COUNT(DISTINCT ') + ') as VARCHAR) ' + ' + ' + '''' + '>' + '''' + ' FROM ' + @tblTarget
exec(@sql)

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39160040
Thank you for the post.

I get an error:

Incorrect syntax near '<'
--------------------

     
    v-----I notice the missing space here
 + ' SELECT  ' + '''' + '<' + '''' + ' + ' + ' CAST(COUNT(DISTINCT '

After adding the required space I ran but got the followng message:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'as'
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39160076
SELECT @sql = 'INSERT INTO ' + @tblTarget + ' SELECT  ' + '''<''' + ' + ' + ' CAST( ' + REPLACE(@where, ',' , ') + COUNT(DISTINCT ') + ') as VARCHAR) + ' + '''>''' + ' FROM ' + @tblTarget

SELECT @sql = replace(@sql,'CAST( ) + ','CAST(')

EXEC (@sql)

Open in new window


it produces:

INSERT INTO YourTable SELECT '<' + CAST(COUNT(DISTINCT a) + COUNT(DISTINCT b) + COUNT(DISTINCT c) as VARCHAR) + '>' FROM YourTable
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39160251
Thank you for the post. When I tried it, I got:

Msg 1035, Level 15, State 10, Line 1
Incorrect syntax near 'CAST', expected 'AS

FYI, before testing, I replaced:

select @sql = '
INSERT INTO ' + @tblTarget + '
SELECT  COUNT(DISTINCT ' +REPLACE(@where,',','),COUNT(DISTINCT ') + ')--,0
FROM ' + @tblTarget
exec(@sql)

with your solution:

SELECT @sql = 'INSERT INTO ' + @tblTarget + ' SELECT  ' + '''<''' + ' + ' + ' CAST( ' + REPLACE(@where, ',' , ') + COUNT(DISTINCT ') + ') as VARCHAR) + ' + '''>''' + ' FROM ' + @tblTarget

SELECT @sql = replace(@sql,'CAST( ) + ','CAST(')
EXEC (@sql)

Mike
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39160323
try this?
declare @sql nvarchar(max), @tblTarget varchar(30), @where varchar(30)
set @tblTarget = 'MyTable'
set @where = ',a,b,c'

SELECT @sql = 'INSERT INTO ' 
            + @tblTarget 
            + '  ([field1]) VALUES ((SELECT  ' + '''' 
            + '<' + '''' 
            + ' + ' 
            + ' CAST( ' + REPLACE(@where, ',' , ') + COUNT(DISTINCT ') + ') as VARCHAR) + ' 
            + '''>''' 
            + ' as field1 FROM ' 
            + @tblTarget
            + '));'

select @sql = replace(@sql,'CAST( ) + ','CAST(')

exec @sql
;

Open in new window

it produces:

INSERT INTO MyTable ([field1]) VALUES ((SELECT '<' + CAST(COUNT(DISTINCT a) + COUNT(DISTINCT b) + COUNT(DISTINCT c) as VARCHAR) + '>' as field1 FROM MyTable));
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39160325
if this does not work, plese provide the table definition you are attempting to count, and to insert into
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39160470
Hi PortletPaul,

Thank you for the time.

Error I got:
Msg 203, Level 16, State 2, Procedure abcd, Line 62
The name 'INSERT INTO MyTable  ([field1]) VALUES ((SELECT  '<' +  CAST(COUNT(DISTINCT a) + COUNT(DISTINCT b) + COUNT(DISTINCT c) as VARCHAR) + '>' as field1 FROM MyTable));' is not a valid identifier.

The entire code see:  http://sqlfiddle.com/#!3/74c24/2

Mike
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39160560
wow, sent me crazy, but it's working... http://sqlfiddle.com/#!3/e2bd2/1

select @sql = 'INSERT INTO ' 
+ @tblTarget 
+ ' SELECT '
+ '''<''' 
+ '+ convert(varchar,COUNT(DISTINCT ' 
+ REPLACE(@where,',',')) + ' + '''>''' + ' ,' + '''<''' + ' + convert(varchar,COUNT(DISTINCT ')
+ ')) + ' 
+ '''>''' 
+ ' FROM '
+ @tblTarget

Open in new window

0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 39161793
Thank you very much. I will post two new questions:

1. Add <tbd> row like:
tbaTarget
========
f1       f2
-------    --------
<3>      <2>
<tbd>   <tbd>
A             P
D             X
E

2. Asking experts somewhat some comments explaning how this stored procedure works. There are some parts is not clear to me.

I will post first one shortly with a link here.

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 39161829
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

839 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