In stored procedures, I am having the following Select statements: There are
similar selects on other tables as well.
.....
.....
Select @v_recordcount = count(*)
From employee
where emp_name = 'Ajay'
If @v_recordcount >0 -- employee exists
begin
end
.....
.....
Now, I want to convert these selects to something like
....
.....
If dbo.check_val('employee','emp_name','Ajay') = 1 --employee exists
begin
end
.....
.....
[quoted text, click to view] "Uri Dimant" wrote:
> Ajay
> Stored Procedure or CHECK Constraints
>
> What are you really trying to do?
>
>
>
> "Ajay" <Ajay@discussions.microsoft.com> wrote in message
> news:58BE5E9C-F1FF-4D96-869C-98FD479BB7EF@microsoft.com...
> > Hi,
> >
> > I would like to create a generic user defined function as follows:
> >
> > check_val(table_name varchar(100), col_name varchar(100), val
> varchar(100))
> >
> > This function will then check the "val" in "col_name" column of
> "table_name"
> > table and returns 0 (not found) or 1 (found).
> >
> > Since, dynamic SQL can't be used inside function, Is there any other
> > possible way to do this task?
> >
> > Really appreciate any help.
> >
> > thanks
>
>
Isn't that this is much straightforward and explicitly explains what it
intend to do:
IF EXISTS(SELECT emp_name FROM employee WHERE emp_name = 'Ajay')
THEN
If dbo.check_val('employee','emp_name','Ajay') = 1
[quoted text, click to view] "Ajay" <Ajay@discussions.microsoft.com> wrote in message
news:54553028-F202-4049-86C6-C18FCDBA679A@microsoft.com...
> In stored procedures, I am having the following Select statements: There
are
> similar selects on other tables as well.
>
> ....
> ....
> Select @v_recordcount = count(*)
> From employee
> where emp_name = 'Ajay'
>
> If @v_recordcount >0 -- employee exists
> begin
>
> end
> ....
> ....
>
> Now, I want to convert these selects to something like
>
> ...
> ....
> If dbo.check_val('employee','emp_name','Ajay') = 1 --employee exists
> begin
>
> end
> ....
> ....
>
> "Uri Dimant" wrote:
>
> > Ajay
> > Stored Procedure or CHECK Constraints
> >
> > What are you really trying to do?
> >
> >
> >
> > "Ajay" <Ajay@discussions.microsoft.com> wrote in message
> > news:58BE5E9C-F1FF-4D96-869C-98FD479BB7EF@microsoft.com...
> > > Hi,
> > >
> > > I would like to create a generic user defined function as follows:
> > >
> > > check_val(table_name varchar(100), col_name varchar(100), val
> > varchar(100))
> > >
> > > This function will then check the "val" in "col_name" column of
> > "table_name"
> > > table and returns 0 (not found) or 1 (found).
> > >
> > > Since, dynamic SQL can't be used inside function, Is there any other
> > > possible way to do this task?
> > >
> > > Really appreciate any help.
> > >
> > > thanks
> >
> >
> >
Ajay
Stored Procedure or CHECK Constraints
What are you really trying to do?
[quoted text, click to view] "Ajay" <Ajay@discussions.microsoft.com> wrote in message
news:58BE5E9C-F1FF-4D96-869C-98FD479BB7EF@microsoft.com...
> Hi,
>
> I would like to create a generic user defined function as follows:
>
> check_val(table_name varchar(100), col_name varchar(100), val
varchar(100))
>
> This function will then check the "val" in "col_name" column of
"table_name"
> table and returns 0 (not found) or 1 (found).
>
> Since, dynamic SQL can't be used inside function, Is there any other
> possible way to do this task?
>
> Really appreciate any help.
>
> thanks
IF @table_name = 'employees'
BEGIN
IF @col_name = 'emp_last_name'
BEGIN
IF EXISTS(SELECT * FROM employees WHERE emp_last_name =
@val)
RETURN 1
ELSE
RETURN 0
END
... repeat for each column that can be searched
END
....repeat for each table that can be searched
As you can imagine, this could be one monster of a UDF.
Do-able, but I'd never touch this.
[quoted text, click to view] "Ajay" <Ajay@discussions.microsoft.com> wrote in message
news:58BE5E9C-F1FF-4D96-869C-98FD479BB7EF@microsoft.com...
> Hi,
>
> I would like to create a generic user defined function as follows:
>
> check_val(table_name varchar(100), col_name varchar(100), val
> varchar(100))
>
> This function will then check the "val" in "col_name" column of
> "table_name"
> table and returns 0 (not found) or 1 (found).
>
> Since, dynamic SQL can't be used inside function, Is there any other
> possible way to do this task?
>
> Really appreciate any help.
>
> thanks