473,804 Members | 3,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sp_executesql & Output Variables. One Ball#$#%% of a problem.

Greetings All, I have a very large query that uses dynamic sql. The
sql is very large and it requires it to be broken into three components
to avoid the nvarchar(4000) issue:

SET @v_SqlString(
N'')

SET @v_SqlString2(
N'')

SET @v_SqlString3(
N'')

The sql is large and I don't have a problem with that so I will not
post it. However, in the last string the very last statement looke
like:
SET @v_SqlString3(
N'
...
...
SELECT @v_TotalRowsLoa ded = @@ROWCOUNT, @v_ExitStat =
@@ERROR
')
I want to catch this output and I am having problems, here is what my
execute looks like:

EXEC('DECLARE @v_TotalRowsLoa ded integer
DECLARE @v_ExitStatus integer
EXEC sp_executesql N''' + @v_SqlString + @v_SqlString2 +
@v_SqlString3 + ''',
N''@v_TotalRows Loaded integer OUTPUT'',
@v_TotalRowsLoa ded OUTPUT,
N''@v_ExitStatu s integer OUTPUT'',
@v_ExitStatus OUTPUT,
N''@v_OLTPQuali fiedPath nvarchar(1000)' ',
@v_OLTPQualifie dPath = ''' + @v_OLTPQualifie dPath + ''',
N''@v_LoadTime datetime'', @v_LoadTime =
''' + @v_LoadTime + '''
'
)
When I run it as is I am prompted with:

Server: Msg 119, Level 15, State 1, Line 126
Must pass parameter number 8 and subsequent parameters as '@name =
value'. After the form '@name = value' has been used, all subsequent
parameters must be passed in the form '@name = value'.
You are required to pass five "5" arguments.
Can anyone tell me why this is failing? What can I do?

Any help would be greatly appreciated.

Jul 23 '05 #1
7 7896
It isn't possible to exceed the 4000 character limit when calling
sp_executesql. I'm guessing your dynamic SQL is failing because it will
be truncated at 4000 characters.

If your code is that complex then I'm certain there must be a better
way than using sp_executesql. Why do you need to do this dynamically at
all? If dynamic code is unavoidable then have you considered using DTS,
..NET or some other host that will let you build dynamic code and handle
return values more easily? Maybe if you explain your goal someone can
give some better suggestions.

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
I didnt' want to get into all of that but what I meant to say was that
the sql string you have to pass sp_executesql must be of type nvarchar
and nvarchar has a limit of 4000 characters. I have to use dynamic sql
for many reasons. 1.) I am am using linked servers and I have to pass
that link server name to the stored proc that is executing the sql
which requires that I used dynamic sql. 2.) The linked server is
connecting to Oracle and because of data issues on the Oracle end I
have to use OPENQUERY() as well. The overall process is an ETL
process and I am doign it via stored procs. The issue I mentioned
above may be happening because I think you cannot have an output
variable with EXEC(). As I have shown I am wrapping executesql with
EXEC() and I do need to grab the exit status and rowcount from the sql
wrapped by @v_SqlString + @v_SqlString2 + @v_SqlString3.

Thanks, TFD

Jul 23 '05 #3
LineVoltageHalo gen (tr************ ****@yahoo.com) writes:
I want to catch this output and I am having problems, here is what my
execute looks like:

EXEC('DECLARE @v_TotalRowsLoa ded integer
DECLARE @v_ExitStatus integer
EXEC sp_executesql N''' + @v_SqlString + @v_SqlString2 +
@v_SqlString3 + ''',
N''@v_TotalRows Loaded integer OUTPUT'',
@v_TotalRowsLoa ded OUTPUT,
N''@v_ExitStatu s integer OUTPUT'',
@v_ExitStatus OUTPUT,
N''@v_OLTPQuali fiedPath nvarchar(1000)' ',
@v_OLTPQualifie dPath = ''' + @v_OLTPQualifie dPath + ''',
N''@v_LoadTime datetime'', @v_LoadTime =
''' + @v_LoadTime + '''
'
)
When I run it as is I am prompted with:

Server: Msg 119, Level 15, State 1, Line 126
Must pass parameter number 8 and subsequent parameters as '@name =
value'. After the form '@name = value' has been used, all subsequent
parameters must be passed in the form '@name = value'.
You are required to pass five "5" arguments.
The error message as such is self-explanatory. You have

@v_OLTPQualifie dPath = <value>, <value>

which is not legal.

But would fix this, you would run into more error messages. You have
declared the parameter list for sp_executesql to have one parameter,
and then you pass a whole bunch. That '@v_ExitStatus integer OUTPUT'
may look like a parameter declaration for you, but it's a value for
a non-existing parameter. The second parameter to sp_executesql must
include all parameters.
1.) I am am using linked servers and I have to pass that link server name
to the stored proc that is executing the sql which requires that I used
dynamic sql.


Hm, depends on how you use that linked server. Remember that a linked
server is really just an alias. Thus, you could use sp_addlinkedser ver
to set your server name to point to the linked server you are about
to talk to.

But if you are into OPENQUERY and needs to parameterize that SQL, you
are in for dynamic SQL anyway.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Erland, thanks for the feedback. Please forgive my lack of
understanding but I am not sure if you are saying that I can do what I
want or if it is not possible? I guess what I am looking for is a bit
of guidance as to how this this thing should look, that is, to make it
work if at all possible?

Regards, TFD.

Jul 23 '05 #5
LineVoltageHalo gen (tr************ ****@yahoo.com) writes:
Erland, thanks for the feedback. Please forgive my lack of
understanding but I am not sure if you are saying that I can do what I
want or if it is not possible? I guess what I am looking for is a bit
of guidance as to how this this thing should look, that is, to make it
work if at all possible?


I think it will work. David say that you cannot cram in more than
4000 characters to sp_executesql, but I he is wrong there. Look at this:

declare @sql1 nvarchar(4000),
@sql2 nvarchar(4000)
select @sql1 = 'SELECT ' + replicate(' ', 3890)
select @sql2 = replicate(' ', 3890) + '4711'
exec ('exec sp_executesql N''' + @sql1 + @sql2 + '''')

Your mistake was that you had spliced up the @params parameter into
several, and also mixed named and positional parameters with each other.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
So, is this the correct form?
EXEC('DECLARE @v_TotalRowsLoa ded integer
DECLARE @v_ExitStatus integer
EXEC sp_executesql N''' + @v_SqlString + @v_SqlString2 +
@v_SqlString3 + ''',
N''@v_TotalRows Loaded integer OUTPUT, @v_ExitStatus integer
OUTPUT,
@v_OLTPQualifie dPath nvarchar(1000)' ', @v_OLTPQualifie dPath = ''' +
@v_OLTPQualifie dPath + ''',
@v_LoadTime datetime'', @v_LoadTime = ''' + @v_LoadTime + '''
''
'
)

Jul 23 '05 #7
LineVoltageHalo gen (tr************ ****@yahoo.com) writes:
So, is this the correct form?
EXEC('DECLARE @v_TotalRowsLoa ded integer
DECLARE @v_ExitStatus integer
EXEC sp_executesql N''' + @v_SqlString + @v_SqlString2 +
@v_SqlString3 + ''',
N''@v_TotalRows Loaded integer OUTPUT, @v_ExitStatus integer
OUTPUT,
@v_OLTPQualifie dPath nvarchar(1000)' ', @v_OLTPQualifie dPath = ''' +
@v_OLTPQualifie dPath + ''',
@v_LoadTime datetime'', @v_LoadTime = ''' + @v_LoadTime + '''
''
'
)


I guess that if it works, it is, and if it doesn't work, it is not. :-)
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4867
by: cliverama | last post by:
help! fried brains.... asp calling a sqlserver7 stored proc which dynamically builds a sqlstatement & passes it to sp_executesql asp page gives the operation not allowed when object is closed error this is the asp code: Set connInc= server.CreateObject("ADODB.Connection") connInc.Open "DSN=db_database;User ID=userid;Password=xxxxxx" Set rsInc= server.CreateObject("ADODB.Recordset")
1
1490
by: Bkr | last post by:
Gurus, Here is what I ma trying to do. I have numeric expression stored in a table column. for e.g. @a + @b + @c. I supply values to the variables at run time and want them to be computed at run time as per the expression in the column. the stored procedure works fine but it gives a silly error. Any help greatly appreciated. Below is the code.
27
3863
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
30
3226
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12 of the C FAQ says that it's wrong for that very reason. What I don't know is where the standard tells me conclusively that it's wrong. What I also don't know is somewhere that this type mismatch will break in practice.
1
8344
by: Matik | last post by:
Hi to all, Probably I'm just doing something stupid, but I would like you to tell me that (if it is so), and point the solution. There ist the thing: I' having a sp, where I call other sp inside. The only problem is, the name of this inside sp is builded variously, and executed over sp_executesql:
1
8257
by: satishchandrat | last post by:
Hi, This is regarding the sp_executesql and the sql statement parameter, in processing a dynamic SQL on SQL Server 2000, in my stored procedure. I have my SQL string exeeding more than 4000 characters. The sp_executesql expects its parameters to be declared as nvarchar/ntext. ntext cannot be declared for a local variable and nvarchar has a maximum limit of 4000 characters. The following was the original approach. ===============
1
1617
by: phoenix917 | last post by:
I have been working on this code for 5 days... I have read multiple tutorials and looked at various code. This is my third forum try... :) My instructor refuses to help me. I think it's because I'm a good student. They assume you know it all. :) Well, I don't and I'm getting a little frustrated and wonder if you guys can shed some light? It's an 8 ball. It works. I've tested and tested but can't find where to put the oldQuestion...
5
3531
by: Yash | last post by:
Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... WHERE <conditions>
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.