Once I insert default values in the table I store the result of scope identity in a variable After this, I have to run some other pieces of code that change the value of scope identity and after running those pieces of code I have to use the value of @id again but it shows an error saying that I
Tag: sql-server
How to properly perform a SELECT query [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 12 months ago. Improve this question I h…
SQL: select max(A), B but don’t want to group by or aggregate B
If I have a house with multiple rooms, but I want the color of the most recently created, I would say: But I get the error: Column ‘room.color’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. If I say max(room.color), then sure, it …
How to add pagination to this dynamic sql
I have the following table with its respective data I wanted to be able to sort the dynamic column example sort by ‘StudentName’ ASC it would sort data using column returning When sorting by StudentName: When sorting by Grade ASC: When sorting by TeacherName ASC (different table) I have manage to …
Group by one column and return several columns on multiple conditions – T-SQL
I have two tables which I can generate with SELECT statements (joining multiple tables) as follows: Table 1: ID Site type time 1 Dallas 2 01-01-2021 2 Denver 1 02-01-2021 3 Chicago 1 03-01-2021 4 Chicago 2 29-11-2020 5 Denver 1 28-02-2020 6 Toronto 2 11-05-2019 Table 2: ID Site collected deposited 1 Denver NU…
Strange Query Plan for SQL Query – Clustered Index Seek
I’ve got a really strange issue with a Query plan generated for a very simple SQL query. The query is searching a full text index, and returning the count of records. For some reason, this SQL query is producing a Non Clustered Scan on an index, which I don’t believe it is optimal to do. I believe…
Check value if exist in another table within Select Query
I have three tables first table stores user basic Info, the second table stores that user who exist in LDAP directory and third table store common information of both tables. below is the table structure I want name from a table if the value is not matched then check to another table. the desired output looks…
SQL Query to determine number of tables
I have this table and I’m trying to determine how many Tables (in a restaurant) are awaiting food. The table( in the database) has the following columns: TableNumber, ObjectType, ObjectDescription and Quantity (amongst others). I made this query to see what is ordered, but would like to know how many un…
Filter on Rows that are Within a Certain Timeframe
I have a table of patients that were admitted and discharged from the hospital. I am trying to find any patients that have an admission within 30 days of their previous discharge date. So for example, I have the table: Name Admission Date Discharge Date Patient A 1/1/2021 1/5/2021 Patient A 1/7/2021 1/10/2021…
Getting null data for left join on same table twice
I have some tables like this: Employee: Teams: TeamMember: I am trying to get list of employees with info regarding who they are : Team Member or Team Leader or Individual. Current output: Expected output: Query: Only issue here is that I am getting TeamId and TeamName as blank for “Team Leader” d…