473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dynamic SQL

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?
Jul 20 '05 #1
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
Jul 20 '05 #2
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
Jul 20 '05 #3
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

Jul 20 '05 #4

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

Similar topics

0
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...
1
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...
6
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...
3
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...
7
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...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
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...
1
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
jinu1996
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...

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.