Attempting to set a non-NULL-able column's value to NULL in sql 2016 - sql-server-2016

Attempting to set a non-NULL-able column's value to NULL" error message is getting when MERGE statement is used in SQL server. This is a known issue in SQL 2008 nad in SQL 2012. (https://support.microsoft.com/en-in/kb/2671078)
Can anyone have idea whether it is fixed in SQL 2016?
Thanks

I ran into this issue today and the problem came down to a broken index on the column I was attempting to display. I had a join on a company ID column that was common to both tables. I was attempting to display the company name from the company master table joined to the invoice table.
I received this error:
Msg 681, Level 16, State 3, Line 12
Attempting to set a non-NULL-able column's value to NULL.
I queried both tables for a NULL company ID and there were none. I queried the company table for NULL company names and there were none. If I removed the column company name from the query, it worked. On the company master table there was an index for the company name column.
I used the estimated execution plan to see that the broken query was using the company name index on the company master table. Since that column was not in the working query I started looking at the indexes.
I tried to rebuild them all and the company name index failed to rebuild with the error:
Attempting to set a non-NULL-able column's value to NULL.
(Microsoft SQL Server, Error: 681)
To resolve, I dropped and recreated the index.

Related

Searching through an entire SQL Server database

I have a SQL Server database that has around 30 tables. I need to be able to search for a keyword (could be string, number, float, date, time) throughout the database and list all the rows where that particular keyword occurs. If it so happens that the keyword is in multiple rows spread across multiple tables, they still need to be displayed. For example, rows containing the keyword from table1 and then some space and then the rows containing the keyword from table 2 and so on.
I have found solutions on this platform such as this: https://stackoverflow.com/a/27361062/13942396
and Narayana Vyas's stored procedure.
But the issue is it displays the table name and te column name and the keyword value. It doesn't display the values of the rest of the columns in that particular row that has the keyword.
Any help would be appreciated!

How to determine if a column exists in SQL server DB in other tables with a different name

I need to fetch specific hardware data from source tables. The hardware information is present in a table Server_Data with columns as follows,
Server_ID, Server Property, Property_Value
65 Model Cisco 123
65 Name Cisco abc
I need to link this table with System table that has columns as follows,
System_ID, System_IP
1 10.20.30.40
I searched all tables in database but Server_ID column is present only in Server _Data table. Also, I searched all tables if there exists a table that links System_ID with Server_ID, but there is no such table.
I need to find if the Server_ID column is present in any other table with some other name (say Server_Key or just Key). Any help would be appreciated.
Only using SQL, there will not be a way to find an identical column in another table, especially if it has a different name. I think you would need to manually compare every column in every other table to that column to find a match.
If I were you I would start by running the following SP:
EXEC sp_fkeys 'Server_Data'
That will tell you if the Server_ID column is referencing any other table in the DB or not.

T-SQL - Using Column Name in where condition without any references when having multiple tables that are engaged using multiple joins

I am a newbie for T-Sql, I came across a SP where multiple tables are engaged using multiple joins but the where clause contain a column field without any table reference and assigned for an incoming variable,like
where 'UserId = #UserId'
instead - no table reference like
'a.UserId = #Userid'`
Can any please do refer to me any material that clears my mind regarding such issue.
If the query works it means that there is only one Column with the name UserId, if there are multiple columns with the same name you have to reference the table too.
If you don't specify the table reference you will get
Ambiguous column name 'UserId'. error
Which means there are more then 2 tables with a column name UserId.
Anyway, always try and use the reference table.

Unable to update a table after adding a column (SQL 2008R2)

We just added two columns, UserHash NVARCHAR(255) and UserSalt NVARCHAR(8), to 4 tables in our database. For ONE of these tables, I am getting the following error about the UserHash column when I try to update anything on that table.
Error Message:
Msg 207, Level 16, State 1, Line 1 Invalid column name 'UserHash'.
Example Update statement:
UPDATE dbo.Users
SET UserSalt = 'asdf', UserHash = 'SomeString'
WHERE ID = 21237
(1 row(s) affected)
(1 row(s) affected)
The tables were edited in SSMS, and 3 of them have had no problem.
When I select, the column is there. Intellisense sees it.
Removing the column restores update capability.
The problem table is only exhibiting the issue on the UserHash column, not on the UserSalt column.
I've tried removing and re-adding. Same problem.
The new columns are not indexed.
I have rebuilt all the indexes on the table.
Every search I do comes up with.. "Oops.. My bad" for answers about queries and views. Have any of you SQL gurus seen this behavior?
Not an Oops my bad, I'm suffering from legacy code from a really bad vendor.. I had to dig pretty deep to find my answer.
The "Audit" tables referenced in the triggers are field independent, however in the trigger for this particular table they use column ordinal position and some other convoluted field tracking that is not compatible with SQL 2008. (http://technet.microsoft.com/en-us/library/ms186329(v=sql.105).aspx) The audit tables and triggers for the other tables are newer, and do not have these same problems.
Now I've got to review 50+ triggers in my DB to bring them up to date.

How to Use dynamic column name in vb.net (with visual studio 2005)

Hi I am creating an attendance system where i want that for a particular id given by user, attendance of that id get saved in the MS access sheet.
What i am wanting is that column name should be i date format pre created by me nad as user enters the id the program retrieves date from system and based on that date (taking it to be dynamic column name) writes an p on them.
I am using SQL query to update i.e using update statement
now i am unable to find correct syntax for using dynamic column name..
So can any one help me??
If I understand what you are saying... I would change the schema you are envisioning to have a table with "ID" and Date Columns. This prevents you from having a fixed and huge number number of date columns.
You insert a new row in this table for an ID and date combination.
I am also assuming you want a report that pivots the data where you have ID in Column position 0 and dates as column headers? Since you are using Access you can create a Cross Tab Query
(pivot) to accomplish this.