I can't seem to crack this one, I will appreciate your assistance!
I'm willing to write a stored procedure that receives a table name (string) and if the table exists, then the procedure should print the number of rows in the table, otherwise, it should print "-1".
I've tried the next code both it keeps printing "-1" even though the procedure is being applied to existing tables:
CREATE PROCEDURE test_pro (@Table_Name varchar(50))
AS
BEGIN
IF OBJECT_ID ('@Table_Name') IS NOT NULL
SELECT COUNT(*)
FROM CONCAT('dbo.', @Table_Name) AS num_rows
ELSE PRINT '-1'
END
EXEC test_pro training_kehila
-- Dropping the procedure
IF OBJECT_ID ('test_pro') IS NOT NULL
DROP PROCEDURE test_pro
Thanks!
training_kehilato the stored procedure but it will always be checking if a table named@Table_Nameexists. To measure the actual row count you'll need dynamic SQL.