Home All Groups Group Topic Archive Search About

Access parameters within dynamic SQL

Author
2 Mar 2006 9:30 PM
McGeeky
I am exploring the use of dynamic SQL within a stored procedure and have run
in to a problem. The dynamic SQL has no visibility of variables declared
outside the dynamic SQL. Try this snippet which causes an error:

declare @branch int
set @branch = 10

exec ( 'select @branch_no' )

Is there any way to make these variables visible to the dynamic sql without
concatenation?

The reason why this will be a problem is that I will be use the openxml
command within the dynamic sql. I will be using very large XML strings so I
am pretty sure that I will have problems concatenating XML strings with
dynamic sql statements.

Any ideas?



Author
2 Mar 2006 10:17 PM
Anith Sen
Lookup the topic sp_ExecuteSQL in SQL Server Books Online. There is an
example which explains how to pass & return values from such strings.
--
Anith
Author
3 Mar 2006 12:31 AM
McGeeky
Great that was really useful. I have combined a couple of examples (openxml
and sp_executesql) from books online in the snippet below to show how XML
can be passed in as a parameter to dynamic sql:

DECLARE @SQLString NVARCHAR(500)

/* Build the SQL string */
SET @SQLString =
     N'
DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

SELECT    *
FROM       OPENXML (@idoc, ''/ROOT/Customer'',1)
            WITH (CustomerID  varchar(10),
                  ContactName varchar(20))

EXEC sp_xml_removedocument @idoc'


/* Execute the string */
EXECUTE sp_executesql @SQLString, N'@doc text',
                      @doc = '<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'


Show quote
"Anith Sen" <an***@bizdatasolutions.com> wrote in message
news:#d272ckPGHA.740@TK2MSFTNGP12.phx.gbl...
> Lookup the topic sp_ExecuteSQL in SQL Server Books Online. There is an
> example which explains how to pass & return values from such strings.
> --
> Anith
>
>
Author
2 Mar 2006 10:20 PM
Aaron Bertrand [SQL Server MVP]
> The reason why this will be a problem is that I will be use the openxml
> command within the dynamic sql. I will be using very large XML strings so
> I
> am pretty sure that I will have problems concatenating XML strings with
> dynamic sql statements.

As long as each string is <= 8000 characters (or 4000 characters with
Unicode), you can say EXEC(@sql1 + @sql2 + @sql3);

A
Author
3 Mar 2006 1:48 AM
--CELKO--
>> am exploring the use of dynamic SQL within a stored procedure and have run
in to a problem. <<

As well you should!!  This is an awful way to even think of writing
code of any kind.  Remember coupling, cohesion and all that stuff in
your fist Software Engineering course?

>> The reason why this will be a problem is that I will be use the openxml command within the dynamic sql. I will be using very large XML strings so I am pretty sure that I will have problems concatenating XML strings with dynamic sql statements. <<

So you want on-the-fly, mixed, proprietary languages so you can
manipulate XML with T-SQL?  This whole thing sounds like a pile of
kludges, but without better specs we can only guess at a relatioanl
solution.
Author
3 Mar 2006 9:06 AM
McGeeky
The reason for dynamic SQL is that I need to parameterize the database name
in the queries. We have a database with 20 odd tables with exactly the same
structure - so rather than duplicating the stored procedure 20 odd times I
am looking at writing it once with dynamic SQL.

The reason for XML is so that I can send large batches of data at a time to
the stored procedure. Which is very efficient.

I am not sure that I will use this technique but it is certainly one of
several I am considering. Its not a position I relish being in but that's
the way the database is so more likely than not I will have to work with its
shortcomings.

See another post by my titled "Parameterize table name without constructing
dynamic query?"

Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1141350534.964106.45490@t39g2000cwt.googlegroups.com...
>>> am exploring the use of dynamic SQL within a stored procedure and have
>>> run
> in to a problem. <<
>
> As well you should!!  This is an awful way to even think of writing
> code of any kind.  Remember coupling, cohesion and all that stuff in
> your fist Software Engineering course?
>
>>> The reason why this will be a problem is that I will be use the openxml
>>> command within the dynamic sql. I will be using very large XML strings
>>> so I am pretty sure that I will have problems concatenating XML strings
>>> with dynamic sql statements. <<
>
> So you want on-the-fly, mixed, proprietary languages so you can
> manipulate XML with T-SQL?  This whole thing sounds like a pile of
> kludges, but without better specs we can only guess at a relatioanl
> solution.
>

AddThis Social Bookmark Button