sql-server-2008

Sql Server union with If condition

天涯浪子 提交于 2021-02-18 09:31:33
问题 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

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

Joining 4 Tables in SQL Server Using Join

南笙酒味 提交于 2021-02-18 02:03:54
问题 Hi Friends I have to join 4 tables in SQL Server and need to Show Specific Coulmns in Data Grid View First Table : emp_details Second Table : tbl_designation Third Table : tbl_empcontribution Forth table : tbl_empdeduction Columns need to show in Data Grid View is 1.From First Table i need to show emp_id, emp_name , emp_pf 2.from Second Table i need to show designation_name 3.from third Table i need to show pfacc1 and pfacc2 4.From Fourth Table i need to show pf_percent and pf_max Try to

Joining 4 Tables in SQL Server Using Join

被刻印的时光 ゝ 提交于 2021-02-18 02:00:52
问题 Hi Friends I have to join 4 tables in SQL Server and need to Show Specific Coulmns in Data Grid View First Table : emp_details Second Table : tbl_designation Third Table : tbl_empcontribution Forth table : tbl_empdeduction Columns need to show in Data Grid View is 1.From First Table i need to show emp_id, emp_name , emp_pf 2.from Second Table i need to show designation_name 3.from third Table i need to show pfacc1 and pfacc2 4.From Fourth Table i need to show pf_percent and pf_max Try to

Customise default 'New Stored Procedure' SSMS 2008 Template

£可爱£侵袭症+ 提交于 2021-02-17 14:46:47
问题 I am trying to customise the default query which is put in place when you click New Stored Procedure... from the Object Explorer on SQL Server Management Studio 2008. I have found how to change the 'Create Stored Procedure (New Menu)' template from the Template Explorer , however this means I will have to keep opening the template explorer rather than clicking on new stored procedure like i usually would. How can I edit the template which appears when you click New Stored Procedure... ? 回答1:

SQL Server heap v.s. clustered index

做~自己de王妃 提交于 2021-02-17 08:41:55
问题 I am using SQL Server 2008. I know if a table has no clustered index, then it is called heap, or else the storage model is called clustered index (B-Tree). I want to learn more about what exactly means heap storage, what it looks like and whether it is organized as "heap" data structure (e.g. minimal heap, maximum heap). Any recommended readings? I want to more a bit more internals, but not too deep. :-) thanks in advance, George 回答1: Heap storage has nothing to do with these heaps. Heap just

SQL Server version error while attaching a mdf file

只谈情不闲聊 提交于 2021-02-17 05:36:03
问题 TITLE: Microsoft SQL Server Management Studio Attach database failed for Server 'DESKTOP-MR6JCUA'. (Microsoft.SqlServer.Smo) ADDITIONAL INFORMATION: An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo) The database 'ABC_Management_System' cannot be opened because it is version 869. This server supports version 852 and earlier. A downgrade path is not supported. Could not open new database 'ABC_Management_System'. CREATE DATABASE is

SQL - Operand data type datetime2 is invalid for subtract operator

微笑、不失礼 提交于 2021-02-16 20:00:07
问题 I want to subtract the value of the first row from the value of the second row of column _timestamp (shown below). _number is the ordering column in my data. and put the result in a new column called diff . I have tried it with the following query use dbtest select t2._number, t2._timestamp, coalesce(t2._timestamp - (select t1._timestamp from dbo.tcp t1 where t1._number = t2._number + 1), t2._timestamp) as diff from dbo.tbl t2 but I am getting the following error. Msg 8117, Level 16, State 1,