[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

T-SQL help... short script

Posted on 2013-05-12
10
Medium Priority
?
246 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
[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
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach 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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
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…

650 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