I’m trying to check if data exists in a table. First I check whether the table exists then I use a SELECT statement to check whether the table is empty or not I have this query which is not supposed to execute SELECT 1 FROM TableName if OBJECT_ID(‘TableName’) returns NULL but this query goes…
Tag: sql-server
Incorrect syntax while accessing SQL table using Python
I’m trying to access a SQL Express database using Python, but I keep getting an incorrect syntax error. I’ve tried a few variations based of other examples I’ve seen, but nothing’s working. The error is pyodbc.ProgrammingError: (‘42000’, “[42000] [Microsoft][ODBC SQL …
How to a check column exits in a table in SQL drill query
In this above query where condition should be executed only when operationType column is present in the table. If operationType column is not present where condition is no needed. Answer Try This:
References invalid table in create table script with foreign keys
I’m trying to create a table in SQL Server with two foreign keys. But the problem is it’s not recognizing one of the reference tables. Here is table creation script. SQL Server is throwing the error on [dbo].[Group]. Here is the error message. I have created both of the tables [dbo].[Group] and [d…
IF when zero result from another Stored Procedure
I have a Stored Procedure which contains an Insert statement on it. From the above Stored Procedure, I want to have a condition from result of another Stored Procedure. With the following action: So, if cBranch is zero. Then execute Sp1 else do nothing. Need advice please. Really appreciated. Answer Try This:
Merge lines over timespan in SCD2 table
I’m having the following table sourced from a SCD2 table. From this source table, I have selected only a few columns, which reults in several lines looking exactly similar. I want to remove the unnecessary lines, those that contain the same data, and have the ValidFrom column showing the first value and…
Get only list of user with latest update by days
I am struggling to write a query to display list of users that check-in in a day, but if the same user check-in twice or more, it would take the latest in a day. For example user A check-in in the morning, then check-in again in the evening, then the system will display only his latest check-in in the list
The SUM() is counting all the values, instead of the ones that meet the condition
I have following query to write: Knowing that a team will earn at least the market value (WartoscRynkowa) of a given player from the sale of a player and knowing the amount of his transfer (KwotaTransferu), calculate how much the team will earn or lose by selling players with the highest chance of transfer (&…
Combine two different rows in single table using same table in SQL Server
Consider a database table holding data like that: Id UserId IsCheckIn DateTime Image AccountId 30356 60866 1 2020-12-19 12:17:17 b622f3e0806f.jpg 10017 30355 60866 0 2020-12-19 10:52:26 b622f3e0806f.jpg 10017 30354 60866 1 2020-12-19 10:51:02 b622f3e0806f.jpg 10017 30353 60866 0 2020-01-20 09:29:42 1596.jpg 1…
sys.tables vs COUNT
I have a variable @tableName (NVARCHAR(255)) that is name of a table. I’m trying to perform an action if data exists in the the table. There are two ways to check this. Method #1 (bulky) Method #2 (clean) Are methods #1 and #2 doing the exact same thing? Is it safe to use method #2? Can there be case wh…