Solved

T-SQL help... short script

Posted on 2013-05-12
10
237 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 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
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…

730 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