Solved

T-SQL help... short script

Posted on 2013-05-12
10
227 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 33

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 48

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
 
LVL 33

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 48

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 48

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 33

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 48

Accepted Solution

by:
PortletPaul earned 500 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 33

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 33

Author Comment

by:Mike Eghtebas
ID: 39161829
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

12 Experts available now in Live!

Get 1:1 Help Now