Unix Technical Forum

dynamic SQL variable with Output ??

This is a discussion on dynamic SQL variable with Output ?? within the SQL Server forums, part of the Microsoft SQL Server category; --> I know this has been dealt with a lot, but I would still really appreciate help. Thanks. I am ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:27 AM
Patrik
 
Posts: n/a
Default dynamic SQL variable with Output ??

I know this has been dealt with a lot, but I would still really
appreciate help. Thanks.

I am trying to transform this table
YY--ID-Code-R1-R2-R3-R4...R40
2004-1-101--1--2-3-4
2004-2-101--2--3-4-2
....
2005-99-103-4-3-2-1

Into a table where the new columns are the count for 4-3-2-1 for every
distinct code in the first table based on year. I will get the year
from the user-end(Access). I will then create my report based on the
info in the new table. Here's what I've tried so far (only for 1st
column):

CREATE PROCEDURE comptabilisationDYN
@colonne varchar(3) '*receives R1, then R2, loop is in vba access*
AS

DECLARE @SQLStatement varchar(8000)
DECLARE @TotalNum4 int
DECLARE @TotalNum3 int
DECLARE @TotalNum2 int
DECLARE @TotalNum1 int

SELECT SQLStatement = 'SELECT COUNT(*) FROM
dbo.Tbl_Réponses_Étudiants WHERE' + @colonne + '=4 AND YY = @year'
EXEC sp_executesql @SQLStatement, N'@TotalNum4 int OUTPUT', @TotalNum4
OUTPUT

INSERT INTO Comptabilisation(Total4) VALUES (@TotalNum4)
GO

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:28 AM
Erland Sommarskog
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??

Patrik (patrik.maheux@umontreal.ca) writes:
> CREATE PROCEDURE comptabilisationDYN
> @colonne varchar(3) '*receives R1, then R2, loop is in vba access*
> AS
>
> DECLARE @SQLStatement varchar(8000)
> DECLARE @TotalNum4 int
> DECLARE @TotalNum3 int
> DECLARE @TotalNum2 int
> DECLARE @TotalNum1 int
>
> SELECT SQLStatement = 'SELECT COUNT(*) FROM
> dbo.Tbl_Réponses_Étudiants WHERE' + @colonne + '=4 AND YY = @year'
> EXEC sp_executesql @SQLStatement, N'@TotalNum4 int OUTPUT', @TotalNum4
> OUTPUT


You need:

SELECT SQLStatement = 'SELECT @TotalNum4 = COUNT(*) FROM
dbo.Tbl_Réponses_Étudiants WHERE' + @colonne + '=4 AND YY = @year'
EXEC sp_executesql @SQLStatement, N'@TotalNum4 int OUTPUT', @TotalNum4
OUTPUT

You also need to add @year to the parameter list.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:28 AM
Patrik
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??

Thank You, but i have tried your code before and again and it still
doesn't work. I call the procedure and it seems to work but it doesn't
write anything in the table. It doesn't recognize the ouput variable in
the INSERT line.

Erland Sommarskog wrote:
> Patrik (patrik.maheux@umontreal.ca) writes:
> > CREATE PROCEDURE comptabilisationDYN
> > @colonne varchar(3) '*receives R1, then R2, loop is in vba access*
> > AS
> >
> > DECLARE @SQLStatement varchar(8000)
> > DECLARE @TotalNum4 int
> > DECLARE @TotalNum3 int
> > DECLARE @TotalNum2 int
> > DECLARE @TotalNum1 int
> >
> > SELECT SQLStatement = 'SELECT COUNT(*) FROM
> > dbo.Tbl_Réponses_Étudiants WHERE' + @colonne + '=4 AND YY =

@year'
> > EXEC sp_executesql @SQLStatement, N'@TotalNum4 int OUTPUT',

@TotalNum4
> > OUTPUT

>
> You need:
>
> SELECT SQLStatement = 'SELECT @TotalNum4 = COUNT(*) FROM
> dbo.Tbl_Réponses_Étudiants WHERE' + @colonne + '=4 AND YY =

@year'
> EXEC sp_executesql @SQLStatement, N'@TotalNum4 int OUTPUT',

@TotalNum4
> OUTPUT
>
> You also need to add @year to the parameter list.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:28 AM
--CELKO--
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are. Sample data is also a good idea, along with clear
specifications.

Your personal narratives and pseudo-code are useless.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 07:28 AM
Erland Sommarskog
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??

Patrik (patrik.maheux@umontreal.ca) writes:
> Thank You, but i have tried your code before and again and it still
> doesn't work. I call the procedure and it seems to work but it doesn't
> write anything in the table. It doesn't recognize the ouput variable in
> the INSERT line.


Could you post the exact code you have now. Looking back on your post,
I see now that there will be a syntax error from the dynamic SQL.

Judging from the code you posted, you should always get a row insered,
even if only a NULL value.

I assume that "seems to work" does not mean that you don't get any
error messages. But maybe you should try running the procedure from
Query Analyzer, in case you have poor error handling in your Access
code.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 07:30 AM
Patrik
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??


Erland Sommarskog wrote:
> Patrik (patrik.maheux@umontreal.ca) writes:
> > Thank You, but i have tried your code before and again and it still
> > doesn't work. I call the procedure and it seems to work but it

doesn't
> > write anything in the table. It doesn't recognize the ouput

variable in
> > the INSERT line.

>
> Could you post the exact code you have now. Looking back on your

post,
> I see now that there will be a syntax error from the dynamic SQL.
>
> Judging from the code you posted, you should always get a row

insered,
> even if only a NULL value.
>
> I assume that "seems to work" does not mean that you don't get any
> error messages. But maybe you should try running the procedure from
> Query Analyzer, in case you have poor error handling in your Access
> code.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp


Thank You, after reading Mr.Sommarskog article more profoundly again
over the week-end, I pick-up my mistake. I wasn't using the parameter
list, now it works fine. Here's my final code.

CREATE PROCEDURE ComptabilisationDYN
@numsaisie int,
@code int,
@colonne varchar(3)
AS

DECLARE @TotalNum4 int
DECLARE @SQL1 nvarchar(1000)
DECLARE @paramlist nvarchar(1000)

SELECT @SQL1 ='SELECT @TotalNum4 = COUNT(*) FROM
dbo.Tbl_Réponses_Étudiants WHERE ' +@colonne + '=''4'''

SELECT @paramlist = '@TotalNum4 int OUTPUT'
EXEC sp_executesql @SQL1, @paramlist, @TotalNum4 OUTPUT

INSERT INTO comptabilisation(Num_Saisie, code, Total4) VALUES
(@numsaisie,@code, @TotalNum4)
GO

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 07:30 AM
Patrik
 
Posts: n/a
Default Re: dynamic SQL variable with Output ??


Erland Sommarskog wrote:
> Patrik (patrik.maheux@umontreal.ca) writes:
> > Thank You, but i have tried your code before and again and it still
> > doesn't work. I call the procedure and it seems to work but it

doesn't
> > write anything in the table. It doesn't recognize the ouput

variable in
> > the INSERT line.

>
> Could you post the exact code you have now. Looking back on your

post,
> I see now that there will be a syntax error from the dynamic SQL.
>
> Judging from the code you posted, you should always get a row

insered,
> even if only a NULL value.
>
> I assume that "seems to work" does not mean that you don't get any
> error messages. But maybe you should try running the procedure from
> Query Analyzer, in case you have poor error handling in your Access
> code.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinf...2000/books.asp


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com