|
its returning the dates greater than 2015-07-01 without any null rrecords for dt_bill_date column of sc_bill table but i want same record to be displayed for dt_open_date column of sc_bill_detail ...thats what my requirement actually
|
|
|
|
|
I suggest you try modifying your code so it iterates through all the results doing the update for each found record.
|
|
|
|
|
i am not writting any code its only a query which i need to write according to the requirement as i told u btw my approach of writting query is correct ?or any other way to get it...coz i think its the simplest one?
modified 23-Sep-15 3:45am.
|
|
|
|
|
Member 11919722 wrote: i think its the simplest one Yes, but it does not work, so it is not the correct one.
|
|
|
|
|
it did work coz i checked the same query with another two tables which i created as an example...n run same query on those two tables ...and getting desired output as well...so its correct one i think
|
|
|
|
|
So if the query works on different tables with the same structure, then you've proved that the problem is with the data in the tables you're using when it doesn't work.
Since we don't have access to your database, we can't diagnose the problem for you. You'll need to check the data in the problem tables to see why the inner query isn't returning a result.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks.... finally i achieved my requirement.
|
|
|
|
|
can u give me ur mail id so that i can send u my tables structure ....
|
|
|
|
|
let me tell you the rwquirement of my query that i have wriiten ...so that u could tell me whether my query is right or wrong according to the requirement..
actually i have two tables sc_bill and sc_bill_details...and i have requirement to get the dates available in sc_bill table ( from july 2015 to till date) to sc_bill_details...is my approch is correct to get the desired output.? i want only query
Thanks
|
|
|
|
|
Read the entire thread to date.
If you run JUST the select:
Member 11919722 wrote: select dt_bill_date from sc_bill where sc_bill.st_bill_no=sc_bill_details.st_bill_no and dt_bill_date>='1-7-2015'
what values do you get? Do you get a record with a NULL date?
|
|
|
|
|
You'll need an inner join to the sc_bill_details table to run the query without the outer UPDATE statement.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Correct... missed that; thanks for catching it.
So... reword the update to be a query with an inner join.
|
|
|
|
|
the thing is that if m writing same query with two defferent tables but not with sc_bill and sc_bill_details....m getting the required output but not with tables that i should get.
|
|
|
|
|
i did as u said n got output also without null values but if m writing the same query...with update statement as outer query the inner query gives foloowing error which is obvious coz inner query returns more than single value ..the query will be as follow as u said right?
update sc_bill_details set dt_open_date=(select dt_bill_date from sc_bill,sc_bill_details where sc_bill.st_bill_no=sc_bill_details.st_bill_no and dt_bill_date>'2015-07-01' )
getting follwing error when wrote above query ....
more than one row returned by a subquery used as an expression
i cant even use in operator with update statement since that will lead to syntax error??
|
|
|
|
|
m not getting any null value if m writing inner query ...
|
|
|
|
|
Hi All,
I want to migrate database from Mysql Server to Sql Server.I migrated database tables structure and Stored Procedures successfully and some database records also by using Sql Server Migration Tool for Mysql.But all the table records not move.
So what I do please give me guidance for this.
|
|
|
|
|
ALTER PROCEDURE [dbo].[SP_SearchByUserNeed]
(
@ColumnName varchar(20),
@Value Varchar(100)
)
AS
BEGIN
BEGIN try
Select * From Product where @ColumnName like @Value+'%';
END try
BEGIN catch
print('Error in [SP_SearchByProductName]')
END catch
END
|
|
|
|
|
You cannot use a variable in this manner. You would need to use dynamic code.
Mongo: Mongo only pawn... in game of life.
|
|
|
|
|
Hay, Thank you so much for your valuable response. I am agree with you. But i can't find a way to do the replacement of the column name dynamically with a variable. If possible please explain the same. I believe. Thank you
|
|
|
|
|
Try something like this - as I am on holiday I have not had a chance to test this:
ALTER PROCEDURE [dbo].[SP_SearchByUserNeed]
(
@ColumnName varchar(20),
@Value Varchar(100)
)
AS
BEGIN
BEGIN try
declare @qry nvarchar(max);
set @qry = 'Select * From Product where @ColumnName like ' + '''' + @Value + '%' + '''';
exec(@qry);
END try
BEGIN catch
print('Error in [SP_SearchByProductName]')
END catch
END
Hugs and kisses to the downvoter
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 16-Sep-15 8:55am.
|
|
|
|
|
That code is vulnerable to SQL injection. To execute dynamic parameterized queries within SQL, you need to use sp_executesql[^].
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
To create dynamic T-SQL you start with a variable for the code
DECLARE @strSQL NVARCHAR(1000);
SET @strSQL = N'Select * From Product where ' + @ColumnName + ' like @Value+''%'';';
EXECUTE sp_executesql @strSQL, @Value
Hope this helps
Mongo: Mongo only pawn... in game of life.
|
|
|
|
|
Almost! You're missing the parameter definition from sql_executesql . I'd also be inclined to validate the column name before concatenating it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It's early here.
Mongo: Mongo only pawn... in game of life.
|
|
|
|
|
This should work:
ALTER PROCEDURE dbo.SP_SearchByUserNeed
(
@ColumnName varchar(20),
@Value varchar(100)
)
AS
BEGIN
DECLARE @RealColumnName sysname;
DECLARE @Statement nvarchar(max);
SELECT
@RealColumnName = name
FROM
sys.columns
WHERE
object_id = OBJECT_ID('Product')
And
name = @ColumnName
;
If @RealColumnName Is Null
BEGIN
RAISERROR('Unknown column: "%s"', 16, 1, @ColumnName);
Return;
END;
SET @Statement = N'SELECT * FROM Product WHERE ' + QuoteName(@RealColumnName) + N' Like @Value + ''%''';
EXEC sp_executesql @Statement, N'@Value varchar(100)', @Value;
END
This will validate that the column name passed in is a valid column in the Product table, and avoid SQL Injection[^] in the dynamic query.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|