Loop through query results

Advertisements DECLARE @files TABLE (DocumentID Varchar(50)) INSERT INTO @files (DocumentID) SELECT DISTINCT D.DocumentID FROM Documents D WHERE D.DocumentID IN (‘637542’, ‘655437’, ‘655900’) –example for debugging — loop DECLARE @i INT = 1 DECLARE @rowCount INT = (SELECT COUNT(*) FROM @files) WHILE @i <= @rowCount BEGIN DECLARE @id Varchar(50) = (SELECT DocumentID FROM @files WHERE ROW_NUMBER()… Read More Loop through query results

Databases in SQL Server

Advertisements enter image description here I have this error in the database I don’t know what can cause this. >Solution : This error means there’s another object, likely a primary key, already in the database. You can search your sys.objects to be sure. Run the following query to see where it’s being used: SELECT OBJECT_NAME(parent_object_id)… Read More Databases in SQL Server

SQL Server EXEC misinterpreting database name

Advertisements This: @query = SELECT Count(*) FROM ReportServer$DEVSVR1.INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ‘BASE TABLE’ executes fine if copied into new query window. But fails EXEC @query with this error: Database ‘SELECT Count(*) FROM ReportServer$DEVSVR1’ does not exist. SQL is incorrectly parsing the phrase ‘SELECT Count(*) FROM ReportServer$DEVSVR1’ as the database name. >Solution : EXEC @query means… Read More SQL Server EXEC misinterpreting database name

SQL Server not allowing datetime column to be added that has a computed default value

Advertisements SQL Server here. Trying to add a DATETIME column to an existing table, that has a default value of "now minus 12 months" (12 months prior to whatever point in time an insert is made to the table). My best attempt: ALTER TABLE myschema.mytable ADD COLUMN Start_Date datetime NOT NULL DEFAULT DATEADD(month, -12, GETDATE())… Read More SQL Server not allowing datetime column to be added that has a computed default value

how can I add up the duplicate values in SQL?

Advertisements I want to know the total number of tweets by the hour but it gives me duplicates. SELECT DISTINCT datepart(hh,tweet_created) AS hours_tweeted, COUNT(tweet_text) AS total_tweets FROM [New_years_resolutions_2020] GROUP BY tweet_created ORDER BY total_tweets DESC; hours_tweeted total_tweets 11 16 11 15 12 14 12 13 I want something like this hours_tweeted total_tweets 11 31 12… Read More how can I add up the duplicate values in SQL?