sql-server-2008-r2

SQL primary key constraint although record does not exist

偶尔善良 提交于 2019-12-02 07:18:50
问题 I am getting the following error: Violation of PRIMARY KEY constraint 'PK_ss_student_grade'. Cannot insert duplicate key in object 'dbo.ss_student_grade'. The duplicate key value is (301, 1011, 24801, 33). If I check the table before the insert there are no records with such a primary key. The insert is done through C# code and I made sure that the code runs only once. even after the error if I check the table I still get no record with such a primary key. Note: a trigger runs on the insert

What is Best Approach for Opening/Closing SqlConnection in C#

こ雲淡風輕ζ 提交于 2019-12-02 07:04:45
I would like to know what could be best approach to open a SqlConnection with Sql Server 2008R2 Express Edition Database. This Version of Sql has Limitations of RAM Usage and CPU Usage so we must adopt something best to open a SqlConnection . Right Now i am Checking Connection on Start and End of each and every Method. Here is an example of that. private void CheckValidId(string Id) { CheckConnectionStatus(); try { sqlConnection.Open(); sqlCommand = new SqlCommand("select * from ValidId where id=@id", sqlConnection); sqlCommand.Parameters.AddWithValue("@id", Id); sqlDataReader = sqlCommand

How do I get a list of columns in a table or view?

社会主义新天地 提交于 2019-12-02 06:45:59
问题 On occasion, I'm interested in getting a list of columns in one of the tables or views in my SQL Server 2008 R2 database. It's useful, for example, if you're building database documentation without using an expensive off-the-shelf product. What's an easy way to get this information? 回答1: In SQL Server 2008 R2 (among other versions), there are system views provided automatically with every database. As long as you are connected to the database where your table resides, you can run a query like

T-SQL Pivot - Total Row and Dynamic Columns

萝らか妹 提交于 2019-12-02 06:14:31
问题 Let's jump straight into it. Here's the code SELECT [prov], [201304], [201305], [201306], [201307] FROM ( SELECT [prov], [arrival], [Amount] FROM [tblSource]) up PIVOT (SUM([Amount]) FOR [arrival] IN ([201304], [201305], [201306], [201307])) AS pvt GO It brings me back an ever so lovely table. I was wondering how I would get the totals for each "date" column to show in an appended last row? In addition, the underlying table will have more data added, specifically more dates. This means that

select with count for years

∥☆過路亽.° 提交于 2019-12-02 06:11:24
I have pickup table which looks like this create table Pickup ( PickupID int IDENTITY, ClientID int , PickupDate date , PickupProxy varchar (200) , PickupHispanic bit default 0, EthnCode varchar(5) , CategCode varchar (2) , AgencyID int, Primary Key (PickupID), ); and it containe pickups for clients I need to create report based on this table which should looks like this I know i need to use CASE but really do not know how to put years and calculate average pickups for each year. and how to count pickups for specific year.so far i have only this SELECT DATEPART(YEAR, PickupDate)as 'Month' FROM

Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

女生的网名这么多〃 提交于 2019-12-02 05:57:43
问题 This is my code var finalResults = (from r in results.AsEnumerable() where r.Field<DateTime>("PrintTime") is DBNull where PrintTime is a column in my Sql Server 2008 r2 database, its type is datetime and it is nullable I got this exception: Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type. Could you help please? 回答1: DataRow.Field supports nullable types, so use DateTime? instead of DateTime : var finalResults = from r in results.AsEnumerable() let printTime = r

SqlDependency.OnChange not firing in WinForm?

耗尽温柔 提交于 2019-12-02 05:43:15
I used Detecting Changes with SqlDependency as example for the code that I'm writing. I've also looked at other links with similar code, but none of them work. Essentially, I simply want to change label1.Text when a change has been made to table [ErrorLog] . For some reason, OnDependencyChange is not firing. I've enabled Service Broker in the database: ALTER DATABASE TestDB SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE Now, here's my complete code. It's very short: public partial class Form1 : Form { private string GetConnectionString() { return @"Data Source=USER-PC\SQLEXPRESS;Initial Catalog

Why does SQL LEN function return '1' for a string with several characters?

空扰寡人 提交于 2019-12-02 04:54:00
问题 Simple question - why when I print the value of the @len variable in the query below would I be getting the value 1, instead of 12 (the number of characters in the specified string)? DECLARE @string varchar DECLARE @index int DECLARE @len int DECLARE @char char(1) SET @string = 'content loop' SET @index = 1 SET @len= LEN(@string) print @len 回答1: Your declaration of @string is wrong. You have no length on the varchar . Try this: declare @string varchar(255); -- or whatever You just learned

Unable to connect to SQL server 2008 R2 via Instance name?

我的梦境 提交于 2019-12-02 04:50:58
问题 I don't understand : I've installed sql server 2008 R2. the installation process asked me for the desired instance name , so I wrote: MSSQLSERVER . after the installation , running Setup Discovery Report does shows me the instance name : I've also installed all the service packs. Also , all the services are up : Also , all the ports are open ( tcp:1433 + udp :1434) Also , Named pipes are on : So , where is the problem ? I'm unable to connect via the instance name : Also unable with : user

SQL Server : calculate monthly total sales incl empty months

旧城冷巷雨未停 提交于 2019-12-02 04:39:35
问题 I'm trying to calculate the total sales of a product in a month, but I would like it to include any "empty" months (with no sales) and only select the latest 12 months. This is my code so far. declare @ProductNo int set @ProductNo = 1234 SELECT YEAR(o.OrderDate) as 'Year', MONTH(o.OrderDate) as 'Month', sum(Amount) as 'Units sold',[ProductNo] FROM [OrderLine] ol inner join [Order] o on ol.OrderNo = o.OrderNo where ProductNo = @ProductNo Group by ProductNo, YEAR(o.OrderDate), Month(o.OrderDate