|
The practical issue here is that if you have an insert trigger that creates an identity record the @@Identity will return the ID from the record created by the trigger whereas @Scope_Identity() will return the ID from the record you re inserting in the current procedure.
This is just another reason why triggers are EVIL!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
can you get the identity without an SP, by issuing two SQL commands separated by a semi-colon, like what the OP seemed trying?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: can you get the identity without an SP
Never having tried it I needed to test this out, the following worked within SSMS. The Identity value should be an output parameter but thats not too difficult.
CREATE TABLE [IDTest](
[ID] [int] IDENTITY(1,1) NOT NULL,
[TextData] [varchar](50) NULL)
DECLARE @SQL VARCHAR(1000)
SET @SQL = 'INSERT IDTest (TextData)VALUES(''Test1'');SELECT @@IDENTITY as ID'
EXEC (@SQL)
DROP TABLE IDTest
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Dear CodeProject GURUs,
Please help me, I want to know that can I use multiple columns for GROUP BY clause after using CASE in GROUP BY? Here is my query.
When I execute my query error occour --> Incorrect syntax near ',' <-- at my GROUP BY clause.
declare @num int
set @num= (select ctrqty from ##temp4temp where ctrqty>1)
print @num
SELECT Distinct b.bl_no,b.laden_date , b.container_code , b.shipper_name ,b.consignee_name,sum(b.qty) As FLQty
FROM docdbdwh.dbo.mastercentral_bl b
INNER JOIN Credo.dbo.account_code ON b.account_code COLLATE DATABASE_DEFAULT = Credo.dbo.account_code.account_code COLLATE DATABASE_DEFAULT
inner JOIN ##tmp_ctr2005111852 c ON b.bl_no COLLATE DATABASE_DEFAULT = c.bl_no COLLATE DATABASE_DEFAULT
And b.container_code COLLATE DATABASE_DEFAULT = c.container_code COLLATE DATABASE_DEFAULT
AND b.vessel_code COLLATE DATABASE_DEFAULT = c.vesselcode COLLATE DATABASE_DEFAULT
And b.voyage_no COLLATE DATABASE_DEFAULT = c.voyageno COLLATE DATABASE_DEFAULT
WHERE ( upper(Credo.dbo.account_code.account_name) like '%FREIGHT%' and upper(Credo.dbo.account_code.account_name) not like '%FREIGHT REBATE%' )
and b.bl_type <> 'S' and b.bl_no Not In (SELECT bl_no FROM docdbdwh.dbo.MasterCentral_BL WHERE BL_ISSUE = 'IDBDO' AND BL_Type = 'T' )
group by case @num
WHEN 2 THEN
(b.bl_no, b.laden_date,b.container_code,b.shipper_name,b.consignee_name,c.status,b.qty)
Else
(b.bl_no, b.laden_date,b.container_code,b.shipper_name,b.consignee_name)
end
Thanks and Best Regards
modified on Thursday, May 20, 2010 5:24 AM
|
|
|
|
|
Apparently it can't be done (not in MS SQL anyway).
You may have to build the whole query as a string first, and add the GROUP BY clause separately, before executing it.
Something like this:
DECLARE @MainQuery as NVARCHAR(MAX)
DECLARE @GROUPBY as NVARCHAR(MAX)
SET @MainQuery = 'SELECT ItemCode, ItemName FROM OITM '
IF 1 = 1
SET @GROUPBY = 'GROUP BY ItemCode, ItemName'
ELSE
SET @GROUPBY = 'GROUP BY ItemCode, ItemName'
SET @MainQuery = @MainQuery + @GROUPBY
EXEC(@MainQuery)
My advice is free, and you may get what you paid for.
|
|
|
|
|
Johan Hakkesteegt wrote:
IF 1 = 1
SET @GROUPBY = 'GROUP BY ItemCode, ItemName'
ELSE
SET @GROUPBY = 'GROUP BY ItemCode, ItemName'
Eh?
|
|
|
|
|
Just an example of a boolean test. I was too lazy to apply it to his code
His' would be:
IF @num = 2
SET @GROUPBY = 'GROUP BY (b.bl_no, b.laden_date,b.container_code,b.shipper_name,b.consignee_name,c.status,b.qty)'
ELSE
SET @GROUPBY = 'GROUP BY (b.bl_no, b.laden_date,b.container_code,b.shipper_name,b.consignee_name)'
My advice is free, and you may get what you paid for.
|
|
|
|
|
Thank you so much. The query work fine with your help.
But, because of my idiot wrong logic the result doesn't make what I want.
|
|
|
|
|
Couple of questions.
Why the use of Distinct ? This doesn't make sense to me.
How can you group by b.qty when it is being summed?
This might vary by database, but trying to group by a field (c.status) that is not being selected is usually an error as well. I think you are trying to get two different results sets from similar queries. It might be better to keep the queries separate, or at least until both of them are working.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
Yes, you are right group by b.qty doesn't make good result.
what I want is sum(b.qty)
SELECT Distinct b.bl_no,b.laden_date , b.container_code , b.shipper_name ,b.consignee_name,sum(b.qty) As FLQty
from T1. Have to join with T2.
So I make
Inner JOIN T2 on T1.F1=T2.F1 and T1.F2=T2.F2 and T1.F3=T2.F3
Sum(b.qty) work fine for F1,F2,F3 from both Table are same.
But, In T2 I have F4(c.status) that field don't have in T1.
c.status have two type T & F.
What I want sum(b.qty) is to sum c.status(T) one line and c.status(F) one line.
My query recently is sum both c.status(T) and c.status(F) into one line.
Please any idea for this? I am weak in query.
|
|
|
|
|
You could look into UNION and UNION ALL .
That way you can combine similar queries into a single result set.
The basic idea is:
SELECT Field1, SUM(Field2) AS 'Total'
FROM Table1
WHERE Field1 > 0
GROUP BY Field1
UNION ALL
SELECT Field3, SUM(Field4) AS 'Total'
FROM Table2
WHERE Field5 < 1000
GROUP BY Field3
UNION ALL
SELECT Field1, SUM(Field6) AS 'Total'
FROM Table3
WHERE Field8 = 20
GROUP BY Field1
My advice is free, and you may get what you paid for.
|
|
|
|
|
You should be able to keep the select as is (although I still don't see a reason for the Distinct clause), but also include in your select list the c.status field. Also include it in the group by and then you will have a row with all the different status values. And if you only want those "T" and "F" statuses, then add that condition to the where clause.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
|
|
|
|
|
When I am trying to connect to a DB in the remote system from my VB6 Application I am getting the error
" Runtime Error -2147467259(80004005)[Microsoft ODBC SQL Server Driver] [DatabaseName] SQL Server does not exist or access denied. "
Pls Help its very urgent
|
|
|
|
|
Well, the error pretty much says it all.
sudheesh kumar s wrote: SQL Server does not exist or access denied.
Check that the SQL Server exists... do you have the name right?
Check that if you have the name right, do you have permission to access it from your location?
|
|
|
|
|
What all I have to check for access permissions. I am using an Online DB and my application is a Windows application in VB6. I have checked the remote connection property of my SQL Server and it is ok. Then i also checked the DB name and connection string (given by the hosting site) and found no errors. Can u pls help me to find out a solution.
Thanks for your quick reply
|
|
|
|
|
Hi all,
I'm plotting a graph using values from the database which i have stored inside.
Below is the sentence i use to extract my x & y axis values.
select temp,result_time from mts300_results where nodeid=5 order by result_time like '%:00:%' desc limit 10;
where temp = x axis
result_time = y axis
wanting the graph to plot a point every hour in the result time(yyyy-mm-dd hh-mm-ss)and where showing the lastest result.
But the graph output time and date is not desc and it all jumble up.
can any one help me to correct my statement?
Any help will be appreciated.
Thanks & Regards
Joseph
|
|
|
|
|
josephong wrote: select temp,result_time from mts300_results where nodeid=5 order by result_time like '%:00:%' desc limit 10;
Take the like out of the order by clause and put it into the where, like this:
select top 10 temp,result_time
from mts300_results
where nodeid=5
and result_time like '%:00:%'
order by result_time desc
|
|
|
|
|
i extract values from POSTGREsql
and its giving error when i use
select temp,result_time from mts300_results where nodeid=5 order by result_time like '%:00:%' desc limit 10;
Error:
DBD::Pg::st execute failed: ERROR: syntax error at or near "10"
LINE 1: select top 10 temp,result_time from mts300_results where nod...
^ at C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/asd.cgi line 19.
My code:
my $dbh = DBI->connect('dbi:Pg:dbname=task;host=localhost', 'tele', 'tiny', { RaiseError => 1, AutoCommit => 0});
my $sql = qq/select top 10 temp,result_time from mts300_results where nodeid=? and result_time like '%:00:%'
order by result_time desc/;
my $sth = $dbh->prepare($sql) or die $!;
$sth->execute(5);
Thanks & Regards
Joseph
|
|
|
|
|
Please read "_Damian S_"'s post again, it pretty much has the solution.
|
|
|
|
|
Hi Experts
I am in great confusion can you help me to overcome
I am having 2 table "TABLE_A" and "TABLE_B" bot have same fields "Field_1","Field_2".
I want to return a result in such a way that compairing both tables with their fields, and return result as "Match" or "Unmatch" in new column
ex:
TABLE_A.Field_1 TABLE_A.Field_2 TABLE_B.Field_1 TABLE_B.Field_2 RESULT
ABC XYZ ABC XYZ Match
PQR XYZ ABC XYZ Unmatch
LOM XYZ LOM XYZ Match
LOM KKR LOM KKR Match
UER SQE OER KKR Unmatch
The Stifler
--
Bugs can neither be created nor be removed from software by a developer. They can only be converted from one form to another. The total number of bugs in the software always remain constant.
|
|
|
|
|
By assuming your field is of varchar...
case when
(TABLE_A.Field_1 + TABLE_A.Field_2) = (TABLE_B.Field_1 + TABLE_B.Field_2)
then 'Match'
else 'Unmatch' end
i guess this will help you .
Reasons are not Important but Results are Important.
http://www.sql4professional.blogspot.com
Swati Tripathi
|
|
|
|
|
hi again
I found following query
SELECT T1.C1,T1.C2,
CASE WHEN (SELECT COUNT(*) FROM TABLE_2 T2 WHERE T1.C1 = T2.C1 AND T1.C2 = T2.C2 ) = 0
THEN 'Unmatch' ELSE 'Match'
END
AS Result
FROM TABLE_1 T1
Still
i am not able to retrieve fields from Table_2 which are not present in Table_1
The Stifler
--
Bugs can neither be created nor be removed from software by a developer. They can only be converted from one form to another. The total number of bugs in the software always remain constant.
|
|
|
|
|
HI pls try below code
Select
C1,
C2,
C3,
C4,
case when (C1 + C2) = (C3 + C4) then 'Match'
else 'Unmatch' end as Result
From
(
Select
Row,
Max(C1) as C1,
Max(C2) as C2,
Max(C3) as C3 ,
Max(C4) as C4
From
(
Select
ROW_NUMBER()
OVER (ORDER BY TABLE_A.Field_1) AS Row,
TABLE_A.Field_1 as C1
,TABLE_A.Field_2 as C2
,'0' as C3
,'0' as C4
From
TABLE_A
Union ALL
Select
ROW_NUMBER()
OVER (ORDER BY TABLE_B.Field_1) AS Row,
'0' as C1
,'0' as C2
,TABLE_B.Field_1 as C3
,TABLE_B.Field_2 as C4
From
TABLE_B
)
as OuterTable
group by Row
) as FinalTable
n yes let me know which sql version u r using because in sql 2008 we have
merge statement too...
i hope this will help you ..
Reasons are not Important but Results are Important.
http://www.sql4professional.blogspot.com
Swati Tripathi
|
|
|
|
|
You have 2 options here. Assume one of the two tables is the "master" and left join the other:
select t1.field1,t1.field2,
t2.field1,t2.field2,
CASE
WHEN t2.field1 IS NOT NULL THEN 'Match'
ELSE 'Unmatch'
END
FROM Table1 t1
LEFT JOIN table2 t2
ON t1.field1=t2.field1 AND t1.field2=t2.field2
or, cross join all of t1 against t2 to find matches.
select t1.field1,t1.field2,
t2.field1,t2.field2,
CASE
WHEN t1.field1=t2.field1 AND t1.field2=t2.field2 THEN 'Match'
ELSE 'Unmatch'
END
FROM Table1 t1
CROSS JOIN table2 t2
Im sure one of those 2 approaches will give you the result you're after.
|
|
|
|
|