|
Hi,
Select * from TABLE Where X Is Null
Pandian S
|
|
|
|
|
Greetings:
I am just cutting my teeth on the business of programming databases using C# and .NET. It doesn't really matter what language because I suspect that the questions I have apply no matter what language is used.
I have found that it is easy enough to use data adaptors or SQL command objects to insert a row into a database table, delete a row or change an existing one. But I have yet to figure out how one creates a table where there was not one before. I find that to create a data adapter, I need to specify the database and the select command I intend to use BEFORE I create the adapter. But what if the table does not yet exist? What if the purpose of the procedure in question is to check for existence of a certain table and create it if its not there?
Could somebody point me to a working example of this being done, if, in fact, it can be done...
Thank you,
Mark
|
|
|
|
|
Jethro63 wrote: But what if the table does not yet exist? What if the purpose of the procedure in question is to check for existence of a certain table and create it if its not there?
You can put just about any SQL commands you like into a SqlCommand. So if you can write it in Query Analyser you can pretty much shove it in a SqlCommand too. The exception is "GO" which is a preprocessor symbol used by Query Analyser to tell it where to split the statements into separate batches. In .NET, the equivalent is to split the SQL up into separate SqlCommands.
So, to check if a table exists:
IF EXISTS (SELECT * FROM INFORMATION_SCHEME.TABLES WHERE TABLE_NAME = 'MyTableName')
BEGIN
-- Do stuff based on the existance of the table
END
ELSE
BEGIN
-- Do stuff based on the nonexistance of the table
END
|
|
|
|
|
Hi Colin. Thanks for your help.
I'm using SQL Desktop server at the moment, so it is difficult for me to try out SQL commands. In an effort to have some sort of proving ground for SQL commands, I use MS-Access and build queries in SQL mode. I realize that there are some differences between the two SQL implementations, but I am not a particularly advanced user and I don't often expect to run into the subleties too often.
Anyway, under Access, I was able to create a table using:
CREATE TABLE StationData;
This worked fine. I'll worry about creating the columns later.
I then attempted to use an SqlCommand object to execute the same statement against my SQL database under C#. I've run into problems here. I am getting SQL exceptions.
First of all, it is not clear to me if I can indeed execute a "CREATE TABLE" SQL statement using SqlCommand. Can I? Which method should I use? "ExecuteNonQuery", "ExecuteScalar"? None of these work.
So, at this point, I don't know if it is simply, my SQL syntax, or I am miss-using SqlCommand object, or if I simply cannot do this with an SqlCommand object.
Can you shed some light? Your help is greatly appreciated.
Cheers,
Mark
|
|
|
|
|
Jethro63 wrote: First of all, it is not clear to me if I can indeed execute a "CREATE TABLE" SQL statement using SqlCommand. Can I?
Yes, you can use any SQL in a SqlCommand .
Jethro63 wrote: Which method should I use? "ExecuteNonQuery", "ExecuteScalar"?
If you don't expect a result set back (as in the case of creating a table) then use ExecuteNonQuery
Jethro63 wrote: So, at this point, I don't know if it is simply, my SQL syntax, or I am miss-using SqlCommand object, or if I simply cannot do this with an SqlCommand object.
Your SQL Syntax is incorrect. You cannot create an empty table, it must have at least one column. You should get a SqlException thrown, it will tell you the error message.
|
|
|
|
|
Thanks Colin. It worked.
The SQL statement that worked was:
CREATE TABLE StationData (Name CHAR(50), Province CHAR(20));
I have one remaining question, if you don't mind:
Since "ExecuteNonQuery" does not return anything, how can I detect if the database already contains the table? I have tried it and it throws an exception and the exception text says that the table already exists. So, is the accepted approach just to catch the exception and make sure that the exception number corresponds to the "Table already exists" error code"?
Thanks again!
Mark
|
|
|
|
|
Jethro63 wrote: how can I detect if the database already contains the table?
You can do it all in SQL if you want:
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='MyTableName')
BEGIN
-- Put table creation code here
END
If you want your .NET application to know about it then something like this:
SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MyTableName'
And use ExecuteScalar() . A result of zero means the table doesn't exist. Any other number indicates the number of tables with that names (you have to add an AND SCHEMA_NAME='NameOfSchema' (e.g. 'dbo' ) if you have multiple schemas.)
|
|
|
|
|
Hi Colin:
I have set up the following SqlCommand object:
<br />
SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = StationData<br />
Visual Studio automatically altered the text of this command to read:
<br />
SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = [StationData]<br />
When I execute this using ExecuteScaler, an exception is thrown. The exception text reads: "Invalid column name 'StationData'".
This is puzzling, as the SQL command is clearly referring to a table, not a column.
Mark
|
|
|
|
|
That is because you have written StationData as if it was a column name. You are performing a SELECT statement on a VIEW , the column name is TABLE_NAME and the type is a SYSNAME which is synonymous with a nvarchar(128) (by the way, all this is in SQL Server books on-line if you look in the index for INFORMATION_SCHEMA.TABLES).
So, when you are filtering on a nvarchar column, you must supply nvarchar data (or something that can be coerced into an nvarchar ). Therefore you need to surround StationData in apostrophes, like this: 'StationData' (just as the example in my previous post showed)
|
|
|
|
|
Yep, that worked.
Thanks again for your help. I will check out this books on-line link that you mentioned.
Cheers,
Mark
|
|
|
|
|
Hi All
I am developing an online shopping web site. The site is similar to any online shopping site where user can select categories from left menu and product will be displayed in middle of page. We had planned to display 6 products per page. As of now we are fine and clear what we need to do.
Main concern is how to bring product data. We are expecting for each category there can be 5000 products and which can grow down the year. Obviously main concern is performance as we are expecting our main users will be dial up users as site will be access from those countries where DSL in not very common. We are in discussion of our product page design and I need your advice which can help me to mitigate any post deployment risks.
I am planning,
1) On Product Page load, I will fetch all the product data (e.g. Product Name, Description, Price, ImagePath, Saleprice, special deals etc…) in DataTable say MyDataTable and cache this DataTable.
2) Now when user clicks on any category from left menus, I will create DataView from cache DataTable by filtering category and bind the DataView with my repeater control.
I believe by this approach I can gain performance hit. For our project performance hit is very critical. Please advice whether I am in correct direction or Not. Please consider this on urgent basis. Thanks in advance for your valuable reply and time
Amit
Amit
|
|
|
|
|
Sql Server is designed to manage memory very well and to execute queries well. Only load the data you will need. Most users will not browse all 5000 products. In a web environment the average response time can be up to a second giving you a lot of flexiblity. Also, keep in mind, write sessionless web apps and you can farm your webservers.
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage
|
|
|
|
|
Hi!
I used to use the MsSQL Express 2005 and used it with the attributes on the Selectcommand. Used to look like this:
SqlCommand Select = new SqlCommand("Select * From _accounts", Connection);
Select.Attributes.Add("@whatever", SqlDbType.VarChar).Value = "whatever";
Like that ..
I can't figure out how to use the Attributes.Add with my MySql connection tho. I'm using the MySql Connector/Net 5.0.0.
I'm greatful for any help .. Thank you!
|
|
|
|
|
I need to import some comma-delimited files into an SQL Server 2005 Express table. I've heard talk that it's only feasible using Access. Is there any other way of doing it? A simple example or a link in the right direction would be greatly appreciated.
"I know which side I want to win regardless of how many wrongs they have to commit to achieve it." - Stan Shannon
Web - Blog - RSS - Math - LinkedIn
|
|
|
|
|
If the format is fairly straightforward you can use bcp . You'll need to define a format file describing the fields in your input file.
|
|
|
|
|
When is a comma-delimited file not straight-forward? Do you mean strings only? Any links on using bcp?
"I know which side I want to win regardless of how many wrongs they have to commit to achieve it." - Stan Shannon
Web - Blog - RSS - Math - LinkedIn
|
|
|
|
|
This is the table I have
C1 C2 C3
1 a NULL
1 b q
1 c f
2 b q
2 d r
2 e z
3 a x
3 f f
I want to select the distinct values from C1 where there is value 'a' in C2 and in C3 the value is NULL or C2 does not contain value 'a' for the value in C1.How do I do that?Here the result would be 1.
|
|
|
|
|
gabriel123456 wrote: I want to select the distinct values from C1
SELECT DISTINCT C1
gabriel123456 wrote: where there is value 'a' in C2 and in C3 the value is NULL
WHERE (C2 = 'a' AND C3 IS NULL)
gabriel123456 wrote: or C2 does not contain value 'a' for the value in C1
OR (C2 <> 'a')
["for the value in C1" does not make sense]
In total:
SELECT DISTINCT C1 WHERE (C2 = 'a' AND C3 IS NULL) OR (C2 <> 'a')
|
|
|
|
|
Hello:
I am now creating an excel file using xslt.However, when there is a blank or a special char in one of my headers, i've got an invalid xpath expression which throw an exception.
what can i do ?
thx
|
|
|
|
|
create a stored porc and i want to send numeric value with parameter how can i do this
create porc test @num int
as
select * from table1 where id in (@num)
i am not use where caluse because i want to send like 1,2,3,4 at the single query
Pls help
vipin paliwal
vipin paliwal
|
|
|
|
|
CREATE PROCEDURE ProcName ( @WhereClause varchar(100) ) AS
-- Create a variable @SQLStatement
DECLARE @SQLStatement varchar(255)
-- Enter the dynamic SQL statement into the
-- variable @SQLStatement
SELECT @SQLStatement = 'select * from table1 where id in ' + @WhereClause
SET NOCOUNT ON
-- Execute the SQL statement
EXEC(@SQLStatement)
SET NOCOUNT OFF
END
Navi
|
|
|
|
|
i create a stored porc and i want to send numeric value with parameter how can i do this
create porc test @num int
as
select * from table1 where id in (@num)
i am not use where caluse because i want to send like 1,2,3,4 at the single query
Pls help
vipin paliwal
-- modified at 3:25 Thursday 24th August, 2006
|
|
|
|
|
create porc - a good start
calus - we can be. Very.
Suggest you post this in the correct forum.
Regards
Malc
*********************************************
|
|
|
|
|
create porc test @num varchar(100)
as
--Set @num='1,2,3,4,5'
Exec('
select * from table1 where id in ('+@num+')
|
|
|
|
|
You're a disgrace.
As another 26 years old, I feel ashamed that you, sir, cannot comprehend the bold, red text saying "Please do NOT post programming questions or ads to this forum." when typing your question at The Lounge.
Aside from that, your text-speak allows me to think of you as someone with lower than average intelligence, lazy, and inconsiderate of others in this community.
Please refrain from further posting until you understand the nettiquete and can write properly.
"A democracy is nothing more than mob rule, where fifty-one percent of the people may take away the rights of the other forty-nine." - Thomas Jefferson
"Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." - Benjamin Franklin
Edbert
Sydney, Australia
|
|
|
|