I have a stored procedure for checking if row exist. I use this with ADO.NET for a dynamic query instead of having similar queries for all my tables.
CREATE PROCEDURE [dbo].[Row_Exist]
@TableName VARCHAR(50),
@ColumnName VARCHAR(50),
@RowId INT
AS
BEGIN
DECLARE @sqlQuery NVARCHAR(MAX) = ''
SET @sqlQuery = 'SELECT COUNT(1) as count FROM ' + @TableName + ' WHERE ' + @ColumnName + ' = ' + @RowId
EXEC(@sqlQuery)
END
If I execute this with a valid rowid, tablename, and columnname, I get the following error
Conversion failed when converting the varchar value
SELECT COUNT(1) as count FROM Users WHERE UserID =to data type int.
EXISTSrather than getting an exactCOUNTand then just checking if it is greater than zero. - HABO