Selecting a Record With MAX Value

Note: An incorrect revision of this answer was edited out. Please review all answers. A subselect in the WHERE clause to retrieve the greatest BALANCE aggregated over all rows. If multiple ID values share that balance value, all would be returned. SELECT ID, BALANCE FROM CUSTOMERS WHERE BALANCE = (SELECT MAX(BALANCE) FROM CUSTOMERS)

T-SQL Decimal Division Accuracy

For multiplication we simply add the number of decimal places in each argument together (using pen and paper) to work out output dec places. But division just blows your head apart. I’m off to lie down now. In SQL terms though, it’s exactly as expected. –Precision = p1 – s1 + s2 + max(6, s1 … Read more

How to get sp_executesql result into a variable?

If you have OUTPUT parameters you can do DECLARE @retval int DECLARE @sSQL nvarchar(500); DECLARE @ParmDefinition nvarchar(500); DECLARE @tablename nvarchar(50) SELECT @tablename = N’products’ SELECT @sSQL = N’SELECT @retvalOUT = MAX(ID) FROM ‘ + @tablename; SET @ParmDefinition = N’@retvalOUT int OUTPUT’; EXEC sp_executesql @sSQL, @ParmDefinition, @retvalOUT=@retval OUTPUT; SELECT @retval; But if you don’t, and can … Read more