I'm writing a stored procedure that uses DynamicSQL using the
following code:
SET @Get_Role_Number = 'Select @Role_Number = ' + @DatabaseName +
'.dbo.sysusers.uid FROM ' + @DatabaseName + '.dbo.sysusers WHERE ' +
@DatabaseName + '.dbo.sysusers.[name] = ' + @ApplicationRole
Execute sp_executeSQL @Get_Role_Number, N'@Role_Number int output,
@DatabaseName varchar(50), @ApplicationRole varchar(50)', @Role_Number
OUTPUT, @DatabaseName, @ApplicationRole
The column name in the table is "name" and I get a message that there
is no such columnname and it gives the value of the parameter passed
as the column name. Is there any way around this? 3 4823
Susan (fe*******@subway.com) writes: I'm writing a stored procedure that uses DynamicSQL using the following code:
SET @Get_Role_Number = 'Select @Role_Number = ' + @DatabaseName + '.dbo.sysusers.uid FROM ' + @DatabaseName + '.dbo.sysusers WHERE ' + @DatabaseName + '.dbo.sysusers.[name] = ' + @ApplicationRole
Execute sp_executeSQL @Get_Role_Number, N'@Role_Number int output, @DatabaseName varchar(50), @ApplicationRole varchar(50)', @Role_Number OUTPUT, @DatabaseName, @ApplicationRole
The column name in the table is "name" and I get a message that there is no such columnname and it gives the value of the parameter passed as the column name. Is there any way around this?
Yes. Did you ever look at the SQL code you generated? When working with
dynamic SQL, it's usually a big time-saver to throw in a @debug parameter
and then
IF @debug = 1
PRINT @Get_Role_Number
You should not include the value of @ApplicationRole in the SQL string,
if you want to pass it as a parameter to the dynamic SQL. You should
include the name of the parameter to the dynamic SQL string.
On the other hand, you cannot pass @DatabaseName as parameter to the
dynamic SQL, but must include it the string, as SQL Server does not
permit variables in these positions.
Also look at http://www.algonet.se/~sommar/dynami...#sp_executesql.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se
Books Online for SQL Server SP3 at http://www.microsoft.com/sql/techinf...2000/books.asp fe*******@subway.com (Susan) wrote in message news:<77**************************@posting.google. com>... I'm writing a stored procedure that uses DynamicSQL using the following code:
SET @Get_Role_Number = 'Select @Role_Number = ' + @DatabaseName + '.dbo.sysusers.uid FROM ' + @DatabaseName + '.dbo.sysusers WHERE ' + @DatabaseName + '.dbo.sysusers.[name] = ' + @ApplicationRole
Execute sp_executeSQL @Get_Role_Number, N'@Role_Number int output, @DatabaseName varchar(50), @ApplicationRole varchar(50)', @Role_Number OUTPUT, @DatabaseName, @ApplicationRole
The column name in the table is "name" and I get a message that there is no such columnname and it gives the value of the parameter passed as the column name. Is there any way around this?
This should work:
declare @Get_Role_Number nvarchar(1000),
@DatabaseName sysname,
@ApplicationRole sysname,
@Role_Number int
set @DatabaseName = 'MyDB'
set @ApplicationRole = 'myRole'
SET @Get_Role_Number = 'Select @Role_Number = uid FROM ' +
@DatabaseName + '.dbo.sysusers WHERE [name] = @ApplicationRole'
Execute sp_executeSQL
@stmt = @Get_Role_Number,
@params = N'@Role_Number int output, @ApplicationRole varchar(50)',
@Role_Number = @Role_Number OUTPUT,
@ApplicationRole = @ApplicationRole
select @Role_Number
Simon
Thanks so much. It worked.
Susan sq*@hayes.ch (Simon Hayes) wrote in message news:<60**************************@posting.google. com>... fe*******@subway.com (Susan) wrote in message news:<77**************************@posting.google. com>... I'm writing a stored procedure that uses DynamicSQL using the following code:
SET @Get_Role_Number = 'Select @Role_Number = ' + @DatabaseName + '.dbo.sysusers.uid FROM ' + @DatabaseName + '.dbo.sysusers WHERE ' + @DatabaseName + '.dbo.sysusers.[name] = ' + @ApplicationRole
Execute sp_executeSQL @Get_Role_Number, N'@Role_Number int output, @DatabaseName varchar(50), @ApplicationRole varchar(50)', @Role_Number OUTPUT, @DatabaseName, @ApplicationRole
The column name in the table is "name" and I get a message that there is no such columnname and it gives the value of the parameter passed as the column name. Is there any way around this?
This should work:
declare @Get_Role_Number nvarchar(1000), @DatabaseName sysname, @ApplicationRole sysname, @Role_Number int
set @DatabaseName = 'MyDB' set @ApplicationRole = 'myRole'
SET @Get_Role_Number = 'Select @Role_Number = uid FROM ' + @DatabaseName + '.dbo.sysusers WHERE [name] = @ApplicationRole'
Execute sp_executeSQL @stmt = @Get_Role_Number, @params = N'@Role_Number int output, @ApplicationRole varchar(50)', @Role_Number = @Role_Number OUTPUT, @ApplicationRole = @ApplicationRole
select @Role_Number
Simon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roel Wuyts |
last post by:
CALL FOR CONTRIBUTIONS
International Workshop on Revival of Dynamic Languages
http://pico.vub.ac.be/~wdmeuter/RDL04/index.html
(at OOPSLA2004, Vancouver, British Columbia, Canada, October...
|
by: Guinness Mann |
last post by:
When you guys talk about "dynamic SQL," to what exactly are you
referring? Is dynamic SQL anything that isn't a stored procedure?
Specifically, I use ASP.NET to communicate with my SQL Server...
|
by: Materialised |
last post by:
Hi Everyone,
I apologise if this is covered in the FAQ, I did look, but nothing
actually stood out to me as being relative to my subject.
I want to create a 2 dimensional array, a 'array of...
|
by: Stephen Gennard |
last post by:
Hello,
I having a problem dynamically invoking a static method that takes a
reference to a SByte*. If I do it directly it works just fine.
Anyone any ideas why?
I have include a example...
|
by: serge |
last post by:
How can I run a single SP by asking multiple sales question either
by using the logical operator AND for all the questions; or using
the logical operator OR for all the questions. So it's always...
|
by: Pascal Costanza |
last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Monday, February 13, 2006, VUB Campus Etterbeek
The VUB (Programming Technology Lab,...
|
by: Mike Livenspargar |
last post by:
We have an application converted from v1.1 Framework to v2.0. The executable
references a class library which in turn has a web reference. The web
reference 'URL Behavior' is set to dynamic. We...
|
by: Peterwkc |
last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back.
By the way, my problem is like this i the first program was...
|
by: bearophileHUGS |
last post by:
I often use Python to write small programs, in the range of 50-500
lines of code. For example to process some bioinformatics data,
perform some data munging, to apply a randomized optimization...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |