Question: Write a script that uses dynamic SQL to return a single column that represents the number of rows in the first table in the current

Write a script that uses dynamic SQL to return a single column that represents the number of rows in the first table in the current database. The script should automatically choose the table that appears first alphabetically, and it should exclude tables named dtproperties and sysdiagrams. Name the column CountOfTable, where Table is the chosen table name. Hint: Use the sys.tables catalog view.

Which queries below correctly answer the above problem statement?

DECLARE @DynamicSQL varchar(2000); SET @DynamicSQL = 'CREATE TABLE tblCount (' SELECT TOP 1 @DynamicSQL = @DynamicSQL + '[' + 'CountOF_' + sys.tables.name + '] int,' FROM sys.tables WHERE name NOT IN ('dtproperties', 'sysdiagrams') SET @DynamicSQL = @DynamicSQL + ');'; EXEC (@DynamicSQL)

DECLARE @TableName varchar(128); SELECT @TableName = MIN(name) FROM sys.tables WHERE name <> 'dtproperties' AND name <> 'sysdiagrams'; EXEC ('SELECT COUNT(*) AS CountOf' + @TableName + ' FROM ' + @TableName + ';');

DECLARE @TotalDue money; SET @TotalDue = (SELECT (InvoiceTotal - PaymentTotal - CreditTotal) As Sum From Invoices); declare @table varchar(50); declare @query varchar(100); set @table = (select top 1 sys.tables.name from sys.tables where name NOT IN ('dtproperties', 'sysdiagrams') order by name asc ); set @query = 'select count(*) as CountOf_' + @table + ' from ' + @table; exec(@query);

DECLARE @TABLENAME NVARCHAR(MAX) = (SELECT TOP 1 sys.tables.name AS CountOf_tablename FROM sys.tables WHERE name NOT IT ('dtproperties', 'sysdiagrams') ORDER BY sys.tables.name ASC) DECLARE @SQL NVCHAR(MAX) = 'SLECT COUNT(*) AS [Total' + @CountOf_tablename + '] FROM ['+@CountOf_tablename]' EXEC(@SQL)

Declare @TableName varchar(128); Select @TableName = Min(name) FROM sys.tables Where name <> 'dtproperties' AND name <> 'sysdiagram'; Print @TableName; EXEC ('SELECT COUNT(*) AS CountOf' + @TableName + ' FROM ' + @TableName + ';');

DECLARE @Row_Count NVARCHAR(1000) SELECT TOP 1 @Row_Count = 'SELECT COUNT(*) [CountOf_' + sys.tables.name + '] FROM ' + sys.tables.name FROM sys.tables WHERE sys.tables.name NOT IN ('dtproperties', 'sysdiagrams') ORDER BY sys.tables.name ASC EXEC(@Row_Count)

SELECT Top 1 name FROM sys.tables WHERE name not like 'dtproperties' and name not like 'sysdiagrams' order by name asc

DELCARE @FirstTable varchar(30) = ( SELECT Top 1 name from sys.tables WHERE name not in 'dtproperties' and 'sysdiagrams' ORDER BY name ASC) DECLARE @CountOf_tablename varchar(50) = 'SELECT COUNT (*) AS [Total + @FirstTable +'] FROM [' + @FirstTable'] EXEC @CountOf_tablename;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!