Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute a function within a string. Possible?

Status
Not open for further replies.

sqlgrade1

Programmer
May 18, 2002
5
CA
here is a tricky question.
let's say i have a string.
declare @functionstring varchar(255)
set @functionstring = 'getdate()'

is there a way to execute this string?
 
There are well-known ways to execute strings. Although I am not especially good at it, here is one way:
exec (@functionstring)

BUT, of course, the command within that string has to itself be executable, right? i.e. you can't just key getdate() into Query Analyzer and expect anything good to happen, because it is not directly executable like that. Likewise, you can set a string = 'getdate()' as you have done, and then try to execute that string, but nothing good is going to happen.

However, here is something that can be done, for example:

declare @functionstring varchar(255)
set @functionstring = 'Select getdate()'
exec (@functionstring)

----------------------
I don't know if I cleared anything up for you or not.
bperry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top