tsql

Sql Server union with If condition

旧时模样 提交于 2021-02-18 09:31:11
问题 I have a query like: DECLARE @tmpValue SET @tmpValue = 0 -- it will be change SELECT * FROM Animal WHERE AniActive = 1 UNION IF @tmpValue > 0 SELECT * FROM Animal WHERE.Active = 0 When I use like this it is giving error because of if condition. I have to use UNION because of our structure. How can I use it with if condition? Thanks, John 回答1: Move the condition @tmpValue > 0 to the WHERE clause like so: SELECT * FROM Animal WHERE AniActive = 1 UNION SELECT * FROM Animal WHERE @tmpValue > 0

Automatically generate a user defined table type that matches an existing table

巧了我就是萌 提交于 2021-02-18 05:16:44
问题 I have several tables that already exist in my database. Some of them have quite a few columns. I want to make some stored procedures to do a merge statement with these tables. To do that I would like the parameter of the stored procedure to be a User Defined Table type. I can go script out each table and modify it to a User Defined Table Type Creation statement. But what I would really like is a way to generate a user defined table type off an existing table in my database. I could then add

Automatically generate a user defined table type that matches an existing table

本秂侑毒 提交于 2021-02-18 05:16:29
问题 I have several tables that already exist in my database. Some of them have quite a few columns. I want to make some stored procedures to do a merge statement with these tables. To do that I would like the parameter of the stored procedure to be a User Defined Table type. I can go script out each table and modify it to a User Defined Table Type Creation statement. But what I would really like is a way to generate a user defined table type off an existing table in my database. I could then add

MERGE - conditional “WHEN MATCHED THEN UPDATE”

梦想的初衷 提交于 2021-02-17 21:14:10
问题 The highlights in the image below shows the logic I want to implement. I realize the syntax is incorrect. Is there a way to conditionally update a record in a MERGE statement only if it the value of one of its columns in the target table is NULL, and the corresponding value in the source table is not null? How would you suggest re-writing this? MERGE dbo.input_311 AS [t] USING dbo.input_311_staging AS [s] ON ([t].[unique key] = [s].[unique key]) WHEN NOT MATCHED BY TARGET THEN INSERT(t.

SUM OVER PARTITION BY

喜你入骨 提交于 2021-02-17 07:50:09
问题 What am I missing? This query is returning duplicate data over and over again. The count is correct for a complete total, but I am expecting one row, and yet I am getting the value repeated about 40 times. Any ideas? SELECT BrandId ,SUM(ICount) OVER (PARTITION BY BrandId ) FROM Table WHERE DateId = 20130618 I get this? BrandId ICount 2 421762 2 421762 2 421762 2 421762 2 421762 2 421762 2 421762 1 133346 1 133346 1 133346 1 133346 1 133346 1 133346 1 133346 What am I missing? I cant remove

Create a stored procedure in SQL Server that returns total count of columns in table and distinct count of values in each column

我只是一个虾纸丫 提交于 2021-02-17 07:24:16
问题 I need a stored procedure that takes any table name as a parameter and returns total count of rows and distinct count of values in each column of that particular table when executed. Let's take the sample table as below: create table journey ( Src varchar(255), Dest varchar(255) ) insert into journey values ('Jaipur', 'Mumbai'), ('Mumbai', 'Jaipur'), ('Kolkata', 'Bangalore'), ('Bangalore', 'Indore'),('Indore', 'Lucknow'), ('Lucknow', 'Indore') Can anybody help me with this task of dynamic SQL

Implementation of [PARTITION BY] command by LINQ

妖精的绣舞 提交于 2021-02-17 06:06:31
问题 I did my best search to convert PARTION BY command from TSQL to LINQ command. But apparently there is no way to convert it. This is my code to which I am going to convert: WITH MyRowSet AS ( SELECT OrderDate ,SalesOrderNumber ,AccountNumber ,CustomerID ,ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY CustomerID, OrderDate DESC) AS RowNum FROM [Sales].[SalesOrderHeader] ) SELECT * FROM MyRowSet WHERE RowNum = 1 If exist any solution I would be greatful to know. 回答1: linq2db has this

Stored Procedure with multi value parameter behaving strangely

穿精又带淫゛_ 提交于 2021-02-17 05:10:12
问题 I created a stored procedure in sql server to feed SSRS to allow it to accept Multiple values. I have created it and when I used it in my report or execute it in sql server I have the following error message. Is there anything i am missing? Thanks Msg 207, Level 16, State 1, Line 35 Invalid column name 'London'. This is my sample data. feel free to create table with it DECLARE @MyTables AS TABLE (ID INT, City VARCHAR(100)) INSERT INTO @MyTables VALUES (1,'London'), (2,'Chester'), (3,'Luton'),

SQL Server: is it possible to get data from another SQL server without setting linked server?

蹲街弑〆低调 提交于 2021-02-16 20:47:31
问题 I need to do the following query (for example): SELECT c1.CustomerName FROM Customer as c1 INNER JOIN [ExternalServer].[Database].[dbo].[Customer] as c2 ON c2.RefId = c1.RefId For some security reason my client doesn't allow me to create a linked server. The user under whom I execute this query has access to both tables. Is it possible to make it work without using linked server? Thanks. 回答1: You could use OPENROWSET, which'll require the connection info, username & password... While I

How to add HIDDEN property on column?

别等时光非礼了梦想. 提交于 2021-02-16 13:37:47
问题 When temporal table is created, we need to defined start and end date time columns which can be hidden - not visible in SELECT * or INSERT without columns . I want to add one more column, which will contain information about the user who has commit the change. The issue is, I am getting the following error: Msg 13735, Level 16, State 1, Line 10 Cannot alter HIDDEN attribute on column 'UserID' in table 'GK' because this column is not a generated always column. Here is the code: DROP TABLE IF