EXEC an INSERT with a dynamic Schema TSQL SSMS

I know that’s a little bit much, but I’m searching for a solution since 4 days. I just want to EXEC a INSERT, but I always get some cryptic Error Messages in SSMS INPUT(variables are wrong so the ErrorLog should get filled): USE [Tfs_GitInterface] GO DECLARE @return_value int EXEC @return_value = [dbo].[spInsertChangeLevel2] @repositoryName = N’asdasd’, […]

Need assistance with a SQL query

I need help with a query that I can’t seem to figure out how to compose. (SQL Server) I have two tables, “Project” and “ProjectStatus” that have these fields: (I have omitted irrelevant columns) Project PROJID (PK) NAME ProjectStatus STATUSID PROJID (FK, references projid in project table) CHANGEDDATE I want to get a list of […]

How to automatically trigger a query at specific time in SQL Server

How can I run this query using auto trigger 5 minutes before the time of next prayer? SELECT DATEADD(minute, -5, (SELECT TOP 1 TIME FROM prayertimes WHERE Time > sysdatetime())) AS TIME Here are details of the prayertimes table: Id P_id TIME ——————————— 698 3 2016-10-08 15:31:00.000 699 4 2016-10-08 18:02:00.000 700 5 2016-10-08 19:32:00.000 […]

How do I sort a SQL Server 2008 pivot result columns and rows?

I’m using This to get pivot results for a table. But I want the resulting columns and rows both in a specific order. So the rows would be sorted by CDATE and the columns would be CDATE, BALANCE, then the DATE columns. CDATE | BALANCE | 04-2007 | 05-2007 | 06-2007 | TRANS TOT 2003-01-15 […]

Find machine where a SQL command was run from

I have a rogue program updating a table in my SQL Server database every few minutes, I can see the transactions in the fn_dblog table. Can anyone tell me how I can trace these back to a specific machine using this fn_dblog or some other means? Thanks.

List of primary key components

I am making a list from a database where I want to show the number of columns that make up the primary key in each particular table in SQL Server. For example, if Table A has a single column as primary key, Table B has a compound key of 3 parts, and Table C has […]

How to run multiple plain sql queries in a single transaction in slick?

I have a Play/Slick application where I need to run multiple plain sql queries in a single transaction retrieving result of the last query like so: val id: Future[Int] = db.run { sql””” DECLARE @T TABLE (id INTEGER) INSERT INTO Foo (name) OUTPUT INSERTED.id INTO @T VALUES (‘bar’) SELECT id FROM @T “””.as[Int].head } The […]

Finding a common string in a string

I have following data in a database: 0203020308260226 020302030826022633 02030203082602263332 0203020308270201 0203020308270222 0203020308290218 02030203082902180208 0203020308290224 02030203082902240208 I wanted to create a query which will pass a value of ‘030203082’ and it will display all the following records. 0203020308260226 020302030826022633 02030203082602263332 0203020308270201 0203020308270222 0203020308290218 02030203082902180208 0203020308290224 02030203082902240208 02030203082902240208 But if I pass ‘0302030829999’ it should also […]

TOP clause slowing down query

I have 2 queries: one with a TOP clause and one without. The result is the exact same yet the one with the TOP clause is significantly slower. Why would that be? Server: Microsoft SQL Server 2008 R2 (SP2) Query 1 – Regular insert into #Buffer (details, persistentID, productID, [date]) select de.details, lg.databaseID % 1000, […]

SQL: How can I get from grouped table also one first row from each group

I have following table CREATE TABLE [dbo].[Table_1]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nchar](10) NULL, [DateOf] [nvarchar](20) NULL ) ON [PRIMARY] and date as: ID|Name|DateOf 1|A|2016-11-29 00:01:00 2|A|2016-11-29 00:02:00 3|A|2016-11-29 00:03:00 4|B|2016-11-29 00:01:00 5|B|2016-11-29 00:02:00 If I make like select name, COUNT(name) from Table_1 group by Name I’ll have A|3 B|2 So, how to get […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.