Ratan
Ratan

Reputation: 883

Passing select statement as parameter to scalar function in SQL

I have scalar function which takes an integer as argument and return an integer too. I was trying to use this function by passing a parameter as a select statement which looks like :

select dbo.scalarFunc(select si.ID from table1 si where version = 9)

It would not let me do it. I tried cast but still did not work. Can anyone tell me if I can use select inside like this or not?

Upvotes: 1

Views: 11636

Answers (1)

Arnaud F.
Arnaud F.

Reputation: 8462

I think a good way to write this is :

select dbo.scalarFunc(table1.ID) from table1 where version = 9

And if you want to use it later :

select * from table2 where ID = (select dbo.scalarFunc(table1.ID) from table1 where version = 9)

Upvotes: 2

Related Questions