Want create a table. The table has some columns that need support:
1.Can contain multiple nulls.
2.If not null, then must be unique.
3.The columns has no relationship.
4.Not entire row is unique. Each column is unique.
How to make the CREATE TABLE command?
It seems SQL server can only support one null when using UNIQUE.
Below example is what I wanted:
CREATE TABLE UniqueTest (
col2 int unique null
INSERT INTO UniqueTest VALUES (1, 1);
INSERT INTO UniqueTest VALUES (2, 2);
INSERT INTO UniqueTest VALUES (3, 2);
INSERT INTO UniqueTest VALUES (4, NULL);
INSERT INTO UniqueTest VALUES (5, NULL);
I had searched internet, lots of articles are discussed in old SQL server version, e.g. 2005/2008.
I think the SQL server 2014/2016 has a new CREATE TABLE option to meet my requirement, but I don't know.
The solution from internet would be:
create index on specific column.
1.My table will has 10~30 columns that need unique and nulls. If each column create an index, then will be 10~30 indexes in a table. Is it possible?
2.Lots indexes will lower efficiency. Is it?
2. Lots of indexes will potentially decrease the performance of inserts and updates, but no more so that any other method of enforcing your requirements, and with less chance of making a mistake.
A table with 30 columns might be a candidate for further normalization. But you would need to examine the data to determine that, and test what effect that might have on the performance and complexity of your queries.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
MS Access,Oracle,MySQL both support unique-nulls on column, but MS SQL server don't.
It is confusing.
The 'CREATE UNIQUE INDEX' solution is traditional. We need a simpler, better and easier way.
So I think SQL server has new keyword option to support unique-nulls column on latest version, e.g. UNIQUE_NULLS in SQL server 2014/1026. But I don't know exact settings on CREATE TABLE. Maybe anyone knows, maybe the new option is not exist at all.
Hi, I have a small program that is for my own use that has a small database. In my program i have a DataGrid to display the information. Right now when i search i have to search via columns, what i want is to search for data contained in the column rows and only display the data searched. Below is the code i have that will return information when a "Column" is entered into the textbox "tbSearch" but I need to be able to search the column rows. I have nine columns that will contain data and i need to search those column rows.
privatevoid button_Click(object sender, RoutedEventArgs e)
using (SqlConnection conn = new SqlConnection())
int result = 0;
string searchOut = tbSearch.Text;
//using (SqlConnection con = new SqlConnection())
conn.ConnectionString = " Data Source=(LocalDB)\\MSSQLLocalDB;Database=cloudyhamdb.mdf;Trusted_Connection=True;AttachDbFilename =|DataDirectory|cloudyhamdb.mdf; Integrated Security = True;";
SqlCommand MyCommand = new SqlCommand("INSERT INTO clouddata " + " (First_Name, Last_Name, Grid_Square, Country, State, Call_Sign, Date_Time, Mode, Power)" + " Values (@First_Name, @Last_Name, @Grid_Square, @Country, @State, @Call_Sign, @Date_Time, @Mode, @Power)", conn);
MyCommand.Parameters["@First_Name"].Value = Convert.ToString(tbFirstName.Text);
MyCommand.Parameters["@Last_Name"].Value = Convert.ToString(tbLastName.Text);
MyCommand.Parameters["@Grid_Square"].Value = Convert.ToString(tbGridSquare.Text);
MyCommand.Parameters["@Country"].Value = Convert.ToString(tbCountry.Text);
MyCommand.Parameters["@State"].Value = Convert.ToString(tbState.Text);
MyCommand.Parameters["@Call_Sign"].Value = Convert.ToString(tbCallSign.Text);
MyCommand.Parameters["@Date_Time"].Value = Convert.ToDateTime(DateTime.Now);
MyCommand.Parameters["@Power"].Value = Convert.ToString(tbPower.Text);
MyCommand.Parameters["@Mode"].Value = Convert.ToString(tbMode.Text);
SqlDataAdapter da = new SqlDataAdapter();
da = new SqlDataAdapter("Select * FROM clouddata", conn);
DataSet ds = new DataSet();
if (tbSearch.Text.Length >= 1)
DataRow returnRows = ds.Tables.Select("Last_Name" + searchOut);
returnRows = ds.Tables.Select("First_Name=" + searchOut);
result = returnRows.Length;
dataGrid2.DataContext = ds.Tables;
int results = MyCommand.ExecuteNonQuery();
Here is an example using a ComboBox and a TextBox. I've used a ComboBox with fixed entries for the user to choose from. I don't want them to enter the column name for a few reasons: They might misspell it, they might introduce other errors and because I'm using string concatenation to get the column name (only the name, NOT the value) it provides a further protection against SQL Injection.
privatevoid button1_Click(object sender, EventArgs e)
if (cmbColumns.SelectedIndex == -1)
MessageBox.Show("You must select a column");
MessageBox.Show("You must enter a value to search for");
// This bit would NOT normally be within the UI layervar conString = ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString;
using (SqlConnection conn = new SqlConnection(conString))
using (var myCommand = new SqlCommand())
myCommand.Connection = conn;
var sb = new StringBuilder(
"SELECT First_Name, Last_Name, Grid_Square, Country, State, Call_Sign, Date_Time, Mode, Power ");
sb.Append("FROM clouddata WHERE ");
myCommand.CommandText = sb.ToString();
using (var adapter = new SqlDataAdapter(myCommand))
var myTable = new DataTable();
dataGridView1.DataSource = myTable;
Hi, I have a problem generating SQL to return a single row for a client. My table structure is the following...
Clients - ClientID, ClientName
Statuses - StatusID, StatusDescription, ClientID
StatusMappings - StatusID, ClientID
The statuses can be different depending on the client, so for example, clients table:
I'm not sure about the example you are presenting ... Are the following statements true?
- The Statuses table shows the possible statuses for each Client?
- The StatusMapping table shows the actual statuses for each client.
If that is the case then with the data you presented you cannot possibly get the results because for Client "Halifax" Status 'Draft Letter' is Id 3 and there is nothing on the StatusMappings table for that Id. Similarly 'Awaiting Response' would be Id 4.
Assuming you get the data sorted out then something like the following will work
SELECT * FROM
SELECT C.ClientID, ClientName, StatusDescription,
CASEWHEN SM.StatusID ISNOTNULLTHEN'Y'ELSE'N'ENDAS Actual
LEFTOUTERJOIN@Statuses S ON C.ClientID = S.ClientID
LEFTOUTERJOIN@StatusMappings SM on SM.StatusID = S.StatusID
MAX(Actual) FOR StatusDescription in ([Draft Letter],[Awaiting Response],[Complete])
WHERE ClientName = 'Barclays'
If you want to use it in an SSRS report then put the query into a Stored Procedure, passing the Client name or id as a parameter.
If the Status Descriptions are not standard or consistent (i.e. not the ones listed) then you will have to use some dynamic sql.
There are articles here on CodeProject on all the topics above
I achieved this in the end by dynamically creating strings to pass in to a SQL statement I was building up. I used MAX and Case statements to achieve this. I finished it an EXEC (@MyString). Thanks for the advice!
Last Visit: 31-Dec-99 18:00 Last Update: 21-Sep-23 0:12