|
You shouldn't need to run 2 queries. You should just need to check the resulting dataset to see if you get any results. (ie. rowcount > 0)
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Thanks for all the help guys.
After reading what you have all posted and doing some more research I managed to solve the problem I was having.
The basic query I was running was sufficient, it turned out the problem was in an automatically created linked query. By fixing that I have got ride of the errors I was getting.
|
|
|
|
|
Joe Stansfield wrote: So would the best approach be to run a query to test if input value is a record, then if true, proceed to the query which returns the record?
The best approach is one without loops. Are you displaying the "linked record" in a separate (child) gridview?
Usually, we combine the tables in Sql, like this;
SELECT a.Field1
,a.Field2
,b.Field1 AS Field3
FROM Employee AS a
JOIN Department AS b ON a.DepartmentId = b.Id
This will generate a single resultset, and depending on the type of join, the database will fetch the employees with, those without a linked record, or both
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Yea I have a linked record showing in a child view.
So from what I can gather the first query executes to fill the main record table, and then a second query populates the correct record in the grid view (and a few other text boxes on the form).
It is working now - but is it worth the time to write it as it should be? It won't affect any of my datasource bindings by combing it into a single query will it?
|
|
|
|
|
Joe Stansfield wrote: It is working now - but is it worth the time to write it as it should be?
It's already as it should be; if you have a separate grid for the children, you'll need a second query. Also explains why the second grid might get an empty resultset.
Joe Stansfield wrote: It won't affect any of my datasource bindings by combing it into a single query will it?
It would! The net effect is that you only pull the data over the network-line once. You're using a different concept here - if you'd fetch all tables, you'd probably have fetched a lot of child-records that the user isn't going to use. That means that your current approach might be the more efficient one, depending on what the user does
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
ITS BETTER U YO USE THE IMMEDIATE CONSTRAINT DURING THE CODING
|
|
|
|
|
Please don't shout (capital letters are considered shouting!).
I would also refrain from using text speak as it upsets a lot of people on these forums.
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
Hi Joe,
You can use following query:
SELECT ISNULL( (SELECT File_Name
FROM Files
WHERE File_Name = @FileName1),1)
This would solve your problem, in case there is no result, it will return you 1 otherwise the matching result.
|
|
|
|
|
There are also a number of other columns for each row, I assume that I just populate the query with those separated by columns?
|
|
|
|
|
Yup
SELECT Col1
,ISNULL(Col2, '1')
,Col3
,Col4
FROM SomeTable
WHERE SomeCondition
..or even nest another select, like this;
SELECT Col1
,ISNULL(Col2, '1')
,(SELECT 1) AS Col3
,Col4
FROM SomeTable
WHERE SomeCondition
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
SELECT isnull(File_Name,1) as FileName from tblname
WHERE (File_Name = @File_Name1)
|
|
|
|
|
Darn, you're right
I was returning the value as a VARCHAR , not an INTEGER - well spotted.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
U can use following statment..
SELECT ISNULL(File_Name, '')
FROM Files
WHERE File_Name = @FileName1
because is is string so it should return blank
|
|
|
|
|
Try the following query.
SELECT CASE WHEN COUNT(File_Name)=0 THEN 1 ELSE COUNT(File_Name) END as FileName from tblname
WHERE (File_Name = @File_Name1)
|
|
|
|
|
if v want to store name like Mohammed's then sql didn't allow to save data and give an error
|
|
|
|
|
Only goes wrong if you concatenate strings, which shouldn't be done in the first place. Use a 'parameterized query';
using (cmd = new MySqlCommand("SELECT EmployeeName FROM Employees WHERE EmployeeId = @P1;"))
{
cmd.Parameters.AddWithValue("@P1", Parameter1);
m.ExecuteNonQuery();
}
Research the AddWithValue member.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
to store name like Mohammed
|
|
|
|
|
|
Hi ,
When i run this Stored Procedure I get this error :
Msg 102, Level 15, State 1, Line 1<br />
Incorrect syntax near 'Pages'.
AND my SP is :
declare @ModuleSettings NVarChar(max) = N'<Article type=''Pages''><Ucl name=''~/UserControls/ModuleSettings/ArticleModuleSettings.ascx''></Ucl><Election>0</Election><IsPaging>False</IsPaging><PageSize>5</PageSize><PagingLocation>Top</PagingLocation><Title>توليد کنندگان مواد اوليه / Suppliers of raw materials</Title><ThumbNail location=''3''>True</ThumbNail><ContentTitle>True</ContentTitle><ContentAbbr>True</ContentAbbr><Icon address=''''>False</Icon><Category>429</Category><All><item align="right" visible="true" order="1" block="true" navigateUrl="~/Management/Modules/Details/">Title</item><item align="right" visible="true" order="2" block="true">Abbr</item><item align="left" visible="true" order="4" block="true">CreateDate</item><item align="right" visible="false" order="5" block="false">Content</item><item align="right" visible="false" order="6" block="false">Keyword</item><item align="right" visible="false" order="7" block="false">Category</item><item align="right" visible="false" order="8" block="false">File</item><item align="right" visible="false" order="9" block="false">Icon</item><item align="right" visible="false" order="10" block="false">writer</item><item align="right" visible="false" order="11" block="false">Source</item></All></Article>'
declare @param nvarchar(max)= N'update [Management].[PageModules] set '
if 100 is not NULL
BEGIN
set @param = @param + '[AreaId]=''' + cast(100 as nvarchar(3)) + ''','
End
if @ModuleSettings is not NULL
BEGIN
set @param = @param + '[ModuleSettings]=''' + @ModuleSettings + ''','
END
if 0 is not NULL
BEGIN
set @param = @param + '[Election]=''' + cast(0 as nvarchar(3)) + ''','
End
if 'sdfg' is not NULL
BEGIN
set @param = @param + '[Title]=''' + 'sdfg' + ''','
End
if 418 is not NULL
BEGIN
set @param = @param + '[CategoryId]=''' + cast(418 as nvarchar(17)) + ''','
End
if NULL is not NULL
BEGIN
set @param = @param + '[DepartmentId]=''' + cast(NULL as nvarchar(9)) + ''','
End
if 38 is not NULL
BEGIN
set @param = @param + '[CultureId]=''' + cast(38 as nvarchar(5)) + ''','
End
if 15 is not NULL
BEGIN
set @param = @param + '[ModuleId]=''' + cast(15 as nvarchar(5)) + ''','
End
if 1 is not NULL
BEGIN
set @param = @param + '[PageId]=''' + cast(1 as nvarchar(9)) + ''','
End
set @param= substring(@param,0,len(@param))
set @param = @param + ' Where Id=''' + cast( 68 as nvarchar(17)) + ''' '
exec sp_executesql @param
modified 6-Nov-12 4:38am.
|
|
|
|
|
Change this;
N'<Article type=''Pages''>
to this;
N'<Article type="Pages">
..and do the same for the "ucl name".
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Building a dynamic query in SQL, even inside a stored procedure, will not prevent SQL injection[^].
Your example could easily be rewritten as a simple UPDATE statement. (NB: Constant values like 100 are never null!) If any of the new values are passed in as parameters, then your query is open to SQL injection.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi
Please help ..
Solutions are not usefull!
but its SP and for example i fill info in that!
|
|
|
|
|
Hi,
I my Self solved the problem via this strategy
Change the bellow code :
if @ModuleSettings is not NULL
BEGIN
set @param = @param + '[ModuleSettings]=''' + <code>@ModuleSettings</code> + ''','
END
With :
if @ModuleSettings is not NULL
BEGIN
set @param = @param + '[ModuleSettings]=''' + <code>replace (@ModuleSettings,CHAR(39),char(39)+CHAR(39))</code> + ''','
End
conclusion :
you should replace :
@ModuleSettings
with :
replace (@ModuleSettings,CHAR(39),char(39)+CHAR(39))
|
|
|
|
|
I believe I've found a very unexpected bug in SQLBulkCopy that took me a while to track down. My test program reads in records from a CSV file with exactly 1 column which is defined as a "numeric(19,5)" for statistical uploads.
Condition 1:
If the input file has 25 rows with the only value being the digit zero, followed by a number with a decimal (like '1.23456'), the data is imported perfectly.
Condition 2:
If the input file has 26 rows with the only value being the digit zero (or any non-decimal number), followed by a number with a decimal (like '1.23456'), the data is imported and the last (and any subsequent) rows have the field imported with truncated decimal digits (like '1.00000').
Condition 3:
If the input file has 26 rows and the first row is '0.0', all of the rows are imported perfectly.
I've checked the table/field definition for Condition 2 and it's the same as 1 and 3 so there's no manipulation of the field type (as far as I can tell).
Is this a bug or am I missing something?
|
|
|
|
|
You say the test file has "1 column".
Then you say that each row has "the digit zero, followed by a number with a decimal (like '1.23456'),"
Does that mean that it has two columns or that the single number is '01.23456'?
Also you should post the following
1. What database version you are using.
2. Example data
3. The relevant code.
4. The database schema you using.
|
|
|
|