logo
down
shadow

Union and If Exists - not working together - Please help


Union and If Exists - not working together - Please help

By : Kamil Najman
Date : November 22 2020, 07:01 PM
may help you . You could add another union that returns the dummy row if test3 is empty:
code :


Share : facebook icon twitter icon
UNION inside IF EXISTS statement not working

UNION inside IF EXISTS statement not working


By : Rahmin Pavlovic
Date : March 29 2020, 07:55 AM
wish helps you , You can transform it like that:
code :
SELECT @dataUserId  = dataUserId FROM TUserData
WHERE DataId = @dataId AND UserId = @userId

IF (@dataUserId IS NOT NULL)
  BEGIN
    SELECT A, B, C FROM TUser
    UNION
    SELECT A, B, C FROM TUser WHERE UserId = @dataUserId 
  END
ELSE
  SELECT A, B, C FROM TUser
Union only if database exists

Union only if database exists


By : Pavan Chopade
Date : March 29 2020, 07:55 AM
like below fixes the issue I have multiple databases with the same tables. I have a table called Invoices. The way I am doing my query now is like: , You can use dynamic SQL for that
code :
DECLARE @sql NVARCHAR(MAX) = ''

IF db_id('Db1') is not null  -- if database Db1 exists
    SET @sql = @sql + 'Select * from [Db1].dbo.Invoices where [Id] = ''someId'''
IF db_id('Db2') is not null
BEGIN
    IF LEN(@sql) > 0
        SET @sql = @sql + N' UNION ALL '
    SET @sql = @sql + 'Select * from [Db2].dbo.Invoices where [Id] = ''someId'''
END
IF db_id('Db3') is not null
BEGIN
    IF LEN(@sql) > 0
        SET @sql = @sql + N' UNION ALL '
    SET @sql = @sql + 'Select * from [Db3].dbo.Invoices where [Id] = ''someId'''
END

exec sp_executesql @sql
Perform a Union as well as a NOT IN/NOT EXISTS

Perform a Union as well as a NOT IN/NOT EXISTS


By : Fulvio Resendes
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further If I understand you correctly, you already have the first part of your set of data from this statement:
code :
;WITH UserProducts AS (
    SELECT [ProductId], [UserId] 
    FROM [Orders] 
    INNER JOIN [OrderItems] ON UserId= [current user id] 
    WHERE [ProductId] = [whatever the current product id is])
    OR [ProductId] IN 
        (SELECT [uidProduct] 
        FROM [ApprovedProductReviews] 
            WHERE [uidProduct]= [current user id])
)
SELECT
    P.*
FROM ProductReviews P
WHERE NOT EXISTS(
    SELECT *
    FROM UserProducts UP
    WHERE UP.ProductId = P.ProductId AND UP.UserId = P.ReviewerProfileId
)
union all with not exists and insert into

union all with not exists and insert into


By : Karim Sayani
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can use MERGE as suggested in comments or just substract from sum existing values.
code :
CREATE TABLE #myTable(col INT);

CREATE TABLE #myTemp(col INT);
CREATE TABLE #someTemp1(col INT);
CREATE TABLE #someTemp2(col INT);

INSERT INTO #myTable(col) VALUES (1), (2), (3);
INSERT INTO #myTemp(col) VALUES (10), (20), (30);
INSERT INTO #someTemp1(col) VALUES (1), (2), (3);
INSERT INTO #someTemp2(col) VALUES (11), (2), (31);

INSERT INTO #myTable(col)
(
  SELECT mt.col FROM #myTemp mt
  UNION ALL
  SELECT col FROM #someTemp1
  UNION ALL
  SELECT col FROM #someTemp2
)
EXCEPT
SELECT col FROM #myTable;


SELECT *
FROM #myTable;
Combine results of three IF Exists conditioned select statements results using union not working in Sql server

Combine results of three IF Exists conditioned select statements results using union not working in Sql server


By : Francis Rodrigues
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I don't know why the union not work for you, but you don't need to use exists if you are using union all :
code :
select t1.col1, t1.col2
from table1 t1
union all
select t2.col1, t2.col2
from table2 t2
union all
select t3.col1, t3.col2
from table3 t3;
Related Posts Related Posts :
  • Msg 209, Level 16, State 1, Line 4 Ambiguous column name 'DonorID'. Msg 209, Level 16, State 1, Line 1 Ambiguous column
  • CASE WHEN Formula in SQL
  • SQL Server Index cost
  • T-SQL Issue using lead or lag
  • How can I connect to SQL Server using (local)\INSTANCE_NAME?
  • How do I filter SQL Server traffic between app and DB servers using Wireshark?
  • How to protect my SQL Server database from unauthorized access, modification and distribution?
  • SQL Server 2008 Dead lock issue
  • writing an Dynamic query in sqlserver
  • How to lock a table for inserting in sql?
  • Disable TSQL script check?
  • is there a way to update multiple row using one update query?
  • Building Stored Procedure to group data into ranges with roughly equal results in each bucket
  • Update table with index is too slow
  • Atomic UPSERT in SQL Server 2005
  • How to run a ssis package using a sql statement?
  • SQL Server substring(substring())
  • Database schema last updated date
  • Fully automated SQL Server Restore
  • How to get identities of inserted data records using SQL bulk copy
  • How to determine whether a database has been changed or not?
  • What does # after a column name mean in SQL Server
  • Transaction Isolation Level of Serializable not working for me
  • SQL Server Management Studio Express 2005 has no Configuration Manager
  • Can't connect to SQL Server 2008 - looks like Shared Memory problem
  • SQL deadlock on delete then bulk insert
  • Join two tables in SQL Server
  • Rebuilding indexes does not change the fragmentation % for nonclustered indexes
  • Identifying connections and active SQL in SQL Server
  • How to pass a variable number of parameters to a SQL Server stored procedure?
  • How do I remove database name from SQL Server generated script?
  • Which method is best method for speed? in SQL Server, stored procedure
  • TSQL 'Invalid column name' error on value of sproc parameter
  • t-SQL grant permission for table drop and create
  • Stored procedure from VBA Excel not running
  • Stored procedure with dynamic SQL and ORDER BY
  • Using SQL Server for WSS 3.0 instead of Windows Internal database
  • Why better isolation level means better performance in SQL Server
  • How to we prevent SQL Injection by Regular Expression or any other way in asp.net
  • Removing characters from a alphanumeric field SQL
  • Using SSIS and T-SQL to convert date to dd.mm.yyyy
  • Is a SqlProfiler Scan Started bad?
  • Copy huge tables data from one database to another in SQL Server
  • Sample Employee SQL Server database with data
  • Programmatically creating linked tables in access
  • Is there meta data I can read from SQL Server to know the last changed row/table?
  • Combine two Column into One column with out union
  • Switch CASE in stored procedure in SQL
  • VS 2019 SSIS package can't add SQL Server
  • Partition or Index large table in SQL Server
  • How to remove only certain items within a column that has multiple words listed in cell
  • How to convert from DD/mm/yyyy in varchar(max) to yyyymmdd format in SQL Server
  • Need to calculate the sum of second using a function from a string variable using SQL
  • Error copying SQL Server database from one server to another
  • SQL Server: link DB2 table
  • Inner Join Query with 2 tables giving different result compared to Sub Query for same 2 tables Northwind database
  • After a specific time of day - which method is better?
  • Select cities from table, which do not start with vowels and do not end with vowels
  • Call stored procedure for each row from select
  • To split records in one column into multiple columns
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk