Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Select Stored Procedure

I want to create a stored procedure based following way:

ALTER PROCEDURE spGetLastId
(
@table_name varchar(100)
)
AS
BEGIN
    DECLARE @DBSql varchar(2000)
    SET @DBSql = 'SELECT MAX(id) as ''LastId'' FROM ' + @table_name
    EXEC sp_executesql @DBSql
END 

This procedure will have to display the last id of any table that I pass as parameter.

Thanks and Regards,

like image 426
mcamara Avatar asked Jan 22 '26 03:01

mcamara


1 Answers

Change @DBSql from varchar to nvarchar and that will work, alternatively look at the built-in IDENT_CURRENT().

like image 95
Alex K. Avatar answered Jan 27 '26 00:01

Alex K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!