Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-27-2008, 01:38 PM
Emerik
 
Posts: n/a
How to use stored procedure parameter in sql-statement?

Hi all,

I'm trying to construct a SP like this:

<code>

CREATE PROCEDURE UpdateInvoice @companyname varchar, @docno int AS
BEGIN
DECLARE @tablename varchar
SET @tablename = '[' + @companyname + '$Inbound Purch_ Document
Header]'
IF ((SELECT COUNT([Inbound Document No_]) FROM @tablename WHERE
[Inbound Document No_] = @docno) > 1)
BEGIN
Print 'Could not find document'
END
ELSE
BEGIN
Print 'Document found'
END
END
GO

</code>

But when I parse the code, I get the following errors:

Server: Msg 137, Level 15, State 2, Procedure UpdateInvoice, Line 6
Must declare the variable '@tablename'.
Server: Msg 156, Level 15, State 1, Procedure UpdateInvoice, Line 11
Incorrect syntax near the keyword 'ELSE'.


I'm quite new to T-SQL and I can't figure out, where the error is. The
variable @tablename HAS BEEN declared (hasn't it?) and what is wrong
near ELSE? Can you please help?

/Emerik
Reply With Quote
  #2 (permalink)  
Old 08-27-2008, 02:17 PM
Plamen Ratchev
 
Posts: n/a
Re: How to use stored procedure parameter in sql-statement?

You cannot use a variable as table name. The solution is to use dynamic
SQL. Read the following article by Erland Sommarskog for detailed
discussion:
http://www.sommarskog.se/dynamic_sql.html

Also, always specify the size of a VARCHAR variable, because when you do
not specify length it is 1 by default.

DECLARE @tablename VARCHAR(35);
SET @tablename = @companyname + '$Inbound Purch_ Document Header';

DECLARE @sql NVARCHAR(2000);

SET @sql = N'SET @cnt = (SELECT COUNT([Inbound Document No_]) FROM ' +
QUOTENAME(@tablename) +
N' WHERE [Inbound Document No_] = @docno)';

DECLARE @params NVARCHAR(50);
SET @params = N'@cnt INT OUTPUT, @docno INT';

DECLARE @cnt INT;
EXEC sp_executesql @sql, @params, @cnt OUTPUT, @docno;

IF @cnt > 1
PRINT 'Could not find document';
ELSE
PRINT 'Document found';

And the logic of the IF statement somehow does not make sense, not sure
why if count is 0 you still consider a document found, and if greater
than 1 it is not found.

--
Plamen Ratchev
http://www.SQLStudio.com
Reply With Quote
  #3 (permalink)  
Old 08-27-2008, 06:18 PM
--CELKO--
 
Posts: n/a
Re: How to use stored procedure parameter in sql-statement?

>> I'm quite new to T-SQL and I can't figure out, where the error is. The variable @tablename HAS BEEN declared (hasn't it?) and what is wrong near ELSE? Can you please help? <<

That has nothing to do with your real problem. Your design is
fundamentally wrong.

In a paper filing system, each company would be given a drawer in a
filing cabinet. In SQL, a table models a set of entities of the same
kind. There should be one and only one table for each set. Instead,
you have made a table for each company! No, wait, a table for each
"'$Inbound_Purch_Document_Header" -- even worse!!

You have a 1930's paper filing system written in SQL. This design
error will force you into dynamic SQL and worse. You need to start
over with a correct data model and schema design. After that, we can
worry about learning proper ISO-111790 naming conventions, code
formatting and so forth.
Reply With Quote
  #4 (permalink)  
Old 08-27-2008, 10:46 PM
Erland Sommarskog
 
Posts: n/a
Re: How to use stored procedure parameter in sql-statement?

Emerik (emerik@emerik.dk) writes:
> CREATE PROCEDURE UpdateInvoice @companyname varchar, @docno int AS
> BEGIN
> DECLARE @tablename varchar
> SET @tablename = '[' + @companyname + '$Inbound Purch_ Document
> Header]'
> IF ((SELECT COUNT([Inbound Document No_]) FROM @tablename WHERE
> [Inbound Document No_] = @docno) > 1)
> BEGIN
> Print 'Could not find document'
> END
> ELSE
> BEGIN
> Print 'Document found'
> END
> END
> GO
>
></code>


Plamen gave you a solution, but I like to emphasise that there is a gross
flaw in the database design here. Companyname should be a column in the
table $Inboun Pucrh_ Document Header, and there should not be one table
per company.


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

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #5 (permalink)  
Old 08-28-2008, 07:58 AM
Emerik
 
Posts: n/a
Re: How to use stored procedure parameter in sql-statement?

Hi Plamen,
Thanks a lot for your fast reply. It worked just perfectly. Beautiful!

> And the logic of the IF statement somehow does not make sense, not sure
> why if count is 0 you still consider a document found, and if greater
> than 1 it is not found.


Oh, yes, you're right! Actually, originally I wrote count <> 1, but I
was unsure about the syntax of "different than", so I wrote "greater
than" instead, because I had an example, where I knew, there was at
least 1 record.
Reply With Quote
  #6 (permalink)  
Old 08-28-2008, 08:07 AM
Emerik
 
Posts: n/a
Re: How to use stored procedure parameter in sql-statement?

> That has nothing to do with your real problem. Your design is
> fundamentally wrong.
>
> There should be one and only one table for each set. *Instead,
> you have made a table for each company! *No, wait, a table for each
> "'$Inbound_Purch_Document_Header" -- even worse!!


Hi again,
Celko and Erlang made some remarks about the "flawful" design.
Actually, I didn't design it, but I'm making an integration towards an
existing database. Surprisingly, it is a standard Navision Attain
database, that is designed in this way.

But thanks for your concern ;-)
Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:29 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Credit Card Consolidation | Facebook Proxy | Montana Music | J j benitez | eHarmony Coupon



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114