sql-server-2012

I need a check constraint on two columns, at least one must be not null

房东的猫 提交于 2019-12-06 17:52:12
问题 I have a table in SQL Server with two numeric columns. At least one of these numeric fields must be filled. How do I write a check constraint to verify this? 回答1: This can be done with a check constraint that verifies null value and matches the result with or create table #t (i int , j int , constraint chk_null check (i is not null or j is not null)) The following are the test cases insert into #t values (null, null) --> error insert into #t values (1, null) --> ok insert into #t values (null

SQL Server dbo.sysdiagrams is a user table or system table

那年仲夏 提交于 2019-12-06 17:02:48
问题 When use Database Diagrams in a simple database, SQL Server create a dbo.sysdiagrams table in the Table\Systam Tables node (in Microsoft management studio\object explorer). But sysdiagrams table marked as user table in SQL Server. you can get user table by below query. SELECT * FROM sys.tables t WHERE OBJECTPROPERTY(t.object_id,'IsUserTable') = 1 I don't know that sysdiagram table is a system table or is a user table. Exists microsoft_database_tools_support with value 1 In the extended

Can't save database default locations in SQL Server

风流意气都作罢 提交于 2019-12-06 16:32:48
问题 I'm trying to change the default directories for data and logs using SQL Server Management Studio. I change the path then click ok but it always reverts back to the old directory in Program Files. Anyone else seen this bug? I'm using SQL Server 2012 Standard. I do see that the correct values are saved in the registry, but they aren't being obeyed by the studio or other connecting applications that would create new databases. 回答1: According to Microsoft, you must restart the SQL Server service

SQL Query - gather data based on date range - possible variable number of columns

可紊 提交于 2019-12-06 16:06:49
please forgive my inexperience, I hope this isn't too dumb of a question, I'm stuck and have no where else to turn. I'll keep it to the point: I'm trying to gather payroll data with the results like so: The issue I have is the variable number of columns . I will be given a date range and are required to return an attendance record for each day in the given range, or a null value if no data is present. I'm using WebAPI as middle tier so I have the ability to perform further data manipulation to achieve this result. My tables are as follows: I can't be the first person who needs this done, any

Grantor does not have GRANT permission - issue

时光毁灭记忆、已成空白 提交于 2019-12-06 15:59:59
I am having a problem with granting proper privileges to a server level role that I created. Whenever a user from the role tries to grant permition to other user I am getting an error like in the title. I did some research and add with grant option when I grant role priveleges but that not helped. I want to keep my role priveleges as low as it must be to perform the given script. Here is how I create my role CREATE SERVER ROLE [My_Server_Role] go GRANT ALTER ANY LOGIN ,ALTER ANY SERVER ROLE ,CONNECT SQL TO [My_Server_Role] WITH GRANT OPTION go now I have a user created like this (and a second

Using Import wizard to import Excel data into table in SQL Server 2012 failing - text truncation

戏子无情 提交于 2019-12-06 15:55:32
I have an Excel file that I import weekly into a table. I am using Excel 2010 and SSMS 2012. The import failed this week and I have been unable to determine why. I was able to successfully import the data into a new table. The table definitions of the new table match the ones of my old table, so that does not seem to be the issue. Below is a screen shot of the error I receive. I have checked the PO header text field and there is nothing that exceeds the datatype of varchar(255) that I have it as. Also, I sorted the data just to see if a certain row was causing the issue and more data did

Detecting file size before sp_send_dbmail

Deadly 提交于 2019-12-06 15:44:39
We're using the following with a 10MB limit for attachments. EXEC msdb..sp_send_dbmail @recipients = 'me@me.co.uk', @subject = 'foo bar', @file_attachments = 'C:\xxx\yyy\myFile.xlsx, @body_format = 'html', @body = 'norf'; Is it possible in the SQL Server scripts to check the size of myFile.xlsx before running this script? I have a solution by using Batch Files: [C:\FileSize.cmd] @echo %~z1 Now you can get size of <your_file> by using that file: DECLARE @t table(filesize int) INSERT INTO @t EXEC [xp_cmdshell] @command_string = 'C:\filesize.cmd <your_file>' SELECT TOP(1) * FROM @t 来源: https:/

SqlClient.SqlException: The wait operation timed out

﹥>﹥吖頭↗ 提交于 2019-12-06 15:03:00
I'm currently trying to create a large amount of test data with numerous insert statements using code similar to below... using (var connection = new SqlConnection(_connectionString)) { using (var command = new SqlCommand(query.ToString(), connection)) { try { connection.Open(); command.ExecuteNonQuery(); return true; } catch (Exception e) { ...... } } } My problem is that I keep getting an error The wait operation timed out and yet when I run the SQL statement that failed from within SQL Server Management Studio, it executes in less than 100ms. The error always seems to occur whilst inserting

How to track changes in records in SQL Server?

大城市里の小女人 提交于 2019-12-06 13:27:40
I have the following table which has tracking records of all students. |==========================================| | ID | Department | Date | |==========================================| | 001 | English | Feb 3 2017 | | 001 | English | Feb 4 2017 | | 001 | Science | Mar 1 2017 | | 001 | Science | Apr 2 2017 | | 001 | Maths | Apr 7 2017 | | 002 | Maths | Feb 1 2017 | | 002 | Maths | Apr 7 2017 | | 003 | Maths | Apr 3 2017 | | 004 | Science | Feb 1 2017 | | 004 | Maths | Apr 7 2017 | |==========================================| I need to fetch the previous record just before when the student

How check that how many stored procedure will affect after deleting one table in SQL [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-06 13:21:27
This question already has answers here : Find all references to an object in an SQL Server database (10 answers) Closed 4 years ago . I am using SQL 2012. I have large Database Structure in my project. I have around 10000 Stored Procedure in my Database. I have to delete one table from the database, Is there any way that I can directly check that by deleteting my table how many stored procedure will affect. Any help Will Be appriciated. knkarthick24 Using sp_depends : EXEC sp_depends @objname = N'yourtablename' If you are using SQL Server Management Studio, you can right click table and select