Showing posts with label syntax. Show all posts
Showing posts with label syntax. Show all posts

Wednesday, March 21, 2012

PLZ help: problem with a certain query

I have this easy query , but the MSSQL server give me an error (Incorrect syntax near ',')

my query is:

--

SELECT Book_Name FROM Books

WHERE (Year,Faculty) =

(SELECT Year,Faculty FROM Books

WHERE Book_Id =100);

--

its seems the error because this line: WHERE (Year,Faculty) =

I don't what the problem!! this query is legal in mysql but not in mssql.

when I tried this one:

SELECT Book_Name FROM Books

WHERE Year =

(SELECT Year FROM Books

WHERE Book_Id =100);

--

its succeed ...

why the first not?

please help or give me any advice....

thanks in advance

Use the following query...

SELECT Book_Name FROM Books Join

(SELECT Year,Faculty FROM Books

WHERE Book_Id =100) D on Books.Year = D.Year and Books.Faculty = D.Faculty

|||

that great ..... thanks you.

but what I can do if I want to use "not in" instead of "=":

the new query will be:

--

SELECT Book_Name FROM Books
WHERE (Year,Faculty) not in
(SELECT Year,Faculty FROM Books
WHERE Book_Id =100);

-

|||

use the following query..

SELECT Book_Name FROM Books Left Outer Join

(SELECT Year,Faculty FROM Books

WHERE Book_Id =100) D on Books.Year = D.Year and Books.Faculty = D.Faculty

Where D.Year is NULL or D.Faculty is NULL

-or

SELECT Book_Name FROM Books Where Cast(Year as varchar) + '-' + Cast(Faculty as varchar) NOT IN

(SELECT Cast(Year as varchar) + '-' + Cast(Faculty as varchar) FROM Books

WHERE Book_Id =100)

|||

thank you a lot.

Worked for me

Monday, March 12, 2012

Pls help me with this SQL CASE WHEN (on ASP)

Hi and thanx for reading my post..

Well.. my problem is that i cannot seem to get the CASE syntax correct within my ASP page. I'm not sure where to break the lines and all that.

I have searched the net for quiiite a while before posting this :)

Will be VERY glad if someone can help me to put the puzzle in the right ASP-way :)

-----START-----
strSQL = "SELECT enavn,fnavn,fodselsnr,fangenr,convert(char(10),reg _date,104) " &_

"'status'= CASE when legal = '1' then "beginner" when legal = '2' then "intermediate" when legal = '3' then "professional" when kan_skal = 'kan' then "darlig foto" else 'status ikke angitt' END " &_

"FROM BILDE WHERE accepted LIKE " & "'1'" & " AND marked LIKE " & "'0'" & " AND pkode LIKE " & strDistrikt & " AND kan_skal LIKE " & "'kan'" & " AND arstall LIKE " & strArstall
-----END------

What i want to do is getting the three values from the SQL DB which are in the "legal" field (1, 2 or 3), and based on what it is display "beginner", "intermediate", "professional" or "darlig foto".

Best regards
Miradoryou have single quotes around status -- remove those

i prefer the as alias syntax instead of the alias = syntax

you have doublequotes around some strings ("beginner") which should be singlequotesselect enavn,fnavn,fodselsnr,fangenr
, convert(char(10),reg_date,104)
, case when legal = '1' then 'beginner'
when legal = '2' then 'intermediate'
when legal = '3' then 'professional'
when kan_skal = 'kan' then 'darlig foto'
else 'status ikke angitt'
end as status
from bilde
where ...also, none of your LIKE strings appear to have wildcards in them|||What language are you using here? What server? I suspect you're terminating your string improperly.

edit: r937 beat me to it.|||Programming in ASP, SQL towards MS SQL.
Tnx for the hints ppl...

I'm not very good at SQL... yet.. could anyone correct my code so that it might work ? thinking about where i should use double-qoutes in the start of lines and where i should use " &_

Mirador

Originally posted by Teddy
What language are you using here? What server? I suspect you're terminating your string improperly.

edit: r937 beat me to it.|||Tnx !!...

Hmm.. btw: what do u mean by Wildcards on the "LIKE"'s ?`

Mir..

Originally posted by r937
you have single quotes around status -- remove those

i prefer the as alias syntax instead of the alias = syntax

you have doublequotes around some strings ("beginner") which should be singlequotesselect enavn,fnavn,fodselsnr,fangenr
, convert(char(10),reg_date,104)
, case when legal = '1' then 'beginner'
when legal = '2' then 'intermediate'
when legal = '3' then 'professional'
when kan_skal = 'kan' then 'darlig foto'
else 'status ikke angitt'
end as status
from bilde
where ...also, none of your LIKE strings appear to have wildcards in them|||Wildcards can differ per application, but in SQL Server an example of a wild card in a like statement would be as follows:

Select legal
from bilde
where accepted like '%&%' (in this case the % are the wild cards).

This would find all records of [legal] from the table [bilde] where the amperstand (&) appears somewhere in the field [accepted].

Originally posted by Mirador
Tnx !!...

Hmm.. btw: what do u mean by Wildcards on the "LIKE"'s ?`

Mir..

Pls help me on the following sql select - I get an error message

Hi
I get an error : ODBC ERROR - Incorrect syntax near , from Crystal SQL
reports.
Your help is really appreciated
SELECT
National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code"
FROM
"nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
National_Oxygen_Pte_Ltd_Customer_Budget
WHERE
(National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_" ,
National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code") NOT IN (SELECT
National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer No_",
National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category Code" FROM
"nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
'2005-03-01 00:00:00.00'} AND
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts '2005-03-3
1
00:00:01.00'}Radha,
Why did you use , at the where condition?
National_Oxygen_Pte_Ltd_Custo=ADmer_Budg
et."Customer No_" ,
National_Oxygen_Pte_Ltd_Custom=ADer_Budg
et."Item Category Code")
Is this a column?

Pls help me on the following sql select

Hi
I get an ODBC ERROR - Incorrect syntax near , from crystal reports 8.5. any
help is realy appreciated
SELECT
National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
National_Oxygen_Pte_Ltd_Customer_Budget."Mar Amount",
National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code",
FROM
"nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
National_Oxygen_Pte_Ltd_Customer_Budget
WHERE
(National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_" ,
National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code") NOT IN (SELEC
T
National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer No_",
National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category Code" FROM
"nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
'2005-03-01 00:00:00.00'} AND
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts '2005-03-3
1
00:00:01.00'}Hi Radha
Just try this way
SELECT
National_Oxygen_Pte_Ltd_Customer_Budget.[Customer No_],
National_Oxygen_Pte_Ltd_Customer_Budget.[Mar Amount],
National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code],
FROM
nox.dbo.[National Oxygen Pte Ltd$Customer Budget]
National_Oxygen_Pte_Ltd_Customer_Budget
WHERE
(National_Oxygen_Pte_Ltd_Customer_Budget
.[Customer No_] ,
National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code]) NOT IN (SELEC
T
National_Oxygen_Pte_Ltd_Sales Invoice_Line.[Customer No_],
National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne.[Item Category Code] FROM
nox.dbo.[National Oxygen Pte Ltd$Sales Invoice Line]
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] >= {ts
'2005-03-01 00:00:00.00'} AND
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] < {ts '2005-03-3
1
00:00:01.00'}
please let me know if there are any issues
thanks and regards
Chandra
"Radharaj" wrote:

> Hi
> I get an ODBC ERROR - Incorrect syntax near , from crystal reports 8.5. an
y
> help is realy appreciated
> SELECT
> National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
> National_Oxygen_Pte_Ltd_Customer_Budget."Mar Amount",
> National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code",
> FROM
> "nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
> National_Oxygen_Pte_Ltd_Customer_Budget
> WHERE
> (National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_" ,
> National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code") NOT IN (SEL
ECT
> National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer No_",
> National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category Code" FROM
> "nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
> '2005-03-01 00:00:00.00'} AND
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts '2005-03
-31
> 00:00:01.00'}|||That won't do if you're hitting against sqlserver.
Sqlserver does not support row constructors so such query will fail.
Try this instead:
SELECT
National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
National_Oxygen_Pte_Ltd_Customer_Budget."Mar Amount",
National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code",
FROM
"nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
National_Oxygen_Pte_Ltd_Customer_Budget
WHERE NOT exists (SELECT *
FROM "nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne
WHERE
National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer
No_" =National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_"
AND National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category
Code" =National_Oxygen_Pte_Ltd_Customer_Budget
."Item Category Code"
AND National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
'2005-03-01 00:00:00.00'} AND
National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts
'2005-03-31 00:00:01.00'}
-oj
"Chandra" <Chandra@.discussions.microsoft.com> wrote in message
news:F136E31C-9006-4817-B854-A0AB994F5E8C@.microsoft.com...
> Hi Radha
> Just try this way
> SELECT
> National_Oxygen_Pte_Ltd_Customer_Budget.[Customer No_],
> National_Oxygen_Pte_Ltd_Customer_Budget.[Mar Amount],
> National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code],
> FROM
> nox.dbo.[National Oxygen Pte Ltd$Customer Budget]
> National_Oxygen_Pte_Ltd_Customer_Budget
> WHERE
> (National_Oxygen_Pte_Ltd_Customer_Budget
.[Customer No_] ,
> National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code]) NOT IN
> (SELECT
> National_Oxygen_Pte_Ltd_Sales Invoice_Line.[Customer No_],
> National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne.[Item Category Code] FROM
> nox.dbo.[National Oxygen Pte Ltd$Sales Invoice Line]
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] >= {ts
> '2005-03-01 00:00:00.00'} AND
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] < {ts
> '2005-03-31
> 00:00:01.00'}
> please let me know if there are any issues
> thanks and regards
> Chandra
>
> "Radharaj" wrote:
>|||Hi Chandra
Thanks for your response. However I am still getting the same error message
Regards
Raj
"Chandra" wrote:
> Hi Radha
> Just try this way
> SELECT
> National_Oxygen_Pte_Ltd_Customer_Budget.[Customer No_],
> National_Oxygen_Pte_Ltd_Customer_Budget.[Mar Amount],
> National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code],
> FROM
> nox.dbo.[National Oxygen Pte Ltd$Customer Budget]
> National_Oxygen_Pte_Ltd_Customer_Budget
> WHERE
> (National_Oxygen_Pte_Ltd_Customer_Budget
.[Customer No_] ,
> National_Oxygen_Pte_Ltd_Customer_Budget.[Item Category Code]) NOT IN (SEL
ECT
> National_Oxygen_Pte_Ltd_Sales Invoice_Line.[Customer No_],
> National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne.[Item Category Code] FROM
> nox.dbo.[National Oxygen Pte Ltd$Sales Invoice Line]
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne WHERE
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] >= {ts
> '2005-03-01 00:00:00.00'} AND
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne.[Shipment Date] < {ts '2005-03
-31
> 00:00:01.00'}
> please let me know if there are any issues
> thanks and regards
> Chandra
>
> "Radharaj" wrote:
>|||Hii oj
thanks a ton!! It works
Regards
Raj
"oj" wrote:

> That won't do if you're hitting against sqlserver.
> Sqlserver does not support row constructors so such query will fail.
> Try this instead:
> SELECT
> National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
> National_Oxygen_Pte_Ltd_Customer_Budget."Mar Amount",
> National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code",
> FROM
> "nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
> National_Oxygen_Pte_Ltd_Customer_Budget
> WHERE NOT exists (SELECT *
> FROM "nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne
> WHERE
> National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer
> No_" =National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_"
> AND National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category
> Code" =National_Oxygen_Pte_Ltd_Customer_Budget
."Item Category Code"
> AND National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
> '2005-03-01 00:00:00.00'} AND
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts
> '2005-03-31 00:00:01.00'}
> --
> -oj
>
> "Chandra" <Chandra@.discussions.microsoft.com> wrote in message
> news:F136E31C-9006-4817-B854-A0AB994F5E8C@.microsoft.com...
>
>|||Dear Oj
Thanks a ton!! It works
Regards
Raj
"oj" wrote:

> That won't do if you're hitting against sqlserver.
> Sqlserver does not support row constructors so such query will fail.
> Try this instead:
> SELECT
> National_Oxygen_Pte_Ltd_Customer_Budget."Customer No_",
> National_Oxygen_Pte_Ltd_Customer_Budget."Mar Amount",
> National_Oxygen_Pte_Ltd_Customer_Budget."Item Category Code",
> FROM
> "nox"."dbo"."National Oxygen Pte Ltd$Customer Budget"
> National_Oxygen_Pte_Ltd_Customer_Budget
> WHERE NOT exists (SELECT *
> FROM "nox"."dbo"."National Oxygen Pte Ltd$Sales Invoice Line"
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne
> WHERE
> National_Oxygen_Pte_Ltd_Sales Invoice_Line."Customer
> No_" =National_Oxygen_Pte_Ltd_Customer_Budget
."Customer No_"
> AND National_Oxygen_Pte_Ltd_Sales_Invoice_Li
ne."Item Category
> Code" =National_Oxygen_Pte_Ltd_Customer_Budget
."Item Category Code"
> AND National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" >= {ts
> '2005-03-01 00:00:00.00'} AND
> National_Oxygen_Pte_Ltd_Sales_invoice_li
ne."Shipment Date" < {ts
> '2005-03-31 00:00:01.00'}
> --
> -oj
>
> "Chandra" <Chandra@.discussions.microsoft.com> wrote in message
> news:F136E31C-9006-4817-B854-A0AB994F5E8C@.microsoft.com...
>
>

Pls help in making this proc work

Hi,

I just found a procedure to make pivot tables,But iam getting an error,can someone help in Overcoming the error.

The syntax is as below

CREATE PROCEDURE crosstab

@.select varchar(8000),

@.sumfunc varchar(100),

@.pivot varchar(100),

@.table varchar(100)

AS

DECLARE @.sql varchar(8000), @.delim varchar(1)

SET NOCOUNT ON

SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @.pivot + ' AS pivot INTO ##pivot FROM ' + @.table + ' WHERE 1=2')

EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @.pivot + ' FROM ' + @.table + ' WHERE '

+ @.pivot + ' Is Not Null')

SELECT @.sql='', @.sumfunc=stuff(@.sumfunc, len(@.sumfunc), 1, ' END)' )

SELECT @.delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )

WHEN 0 THEN '' ELSE '''' END

FROM tempdb.information_schema.columns

WHERE table_name='##pivot' AND column_name='pivot'

SELECT @.sql= @.sql + '''' + convert(varchar(100), pivot) + ''' = ' +

stuff(@.sumfunc,charindex( '(', @.sumfunc )+1, 0, ' CASE ' + @.pivot + ' WHEN '

+ @.delim + convert(varchar(100), pivot) + @.delim + ' THEN ' ) + ', ' FROM ##pivot

DROP TABLE ##pivot

SELECT @.sql=left(@.sql, len(@.sql)-1)

SELECT @.select=stuff(@.select, charindex(' FROM ', @.select)+1, 0, ', ' + @.sql + ' ')

EXEC (@.select)

SET ANSI_WARNINGS ON

just run the procedure and help me to fix the error.The error iam getting is

Msg 156, Level 15, State 1, Procedure crosstab, Line 23

Incorrect syntax near the keyword 'pivot'.

Any help is greatly apprecited.

Thanks,

SVGP

We will need a little more information to help you debug this.

Please provide DDL (Create statements for any tables involved), Sample data (In the form of Insert statements) and the command you are using to execute this stored Procedure (Including parameters)

|||

Hi,

There are so many tables involved with lots of data.

But the problem is not during the execution of proc,its while creating the proc,which doesnot involve any of the tables.

If still you require i will send them,

Thanks,

SVGP

|||

Where you have:

convert(varchar(100), pivot)

Change to

convert(varchar(100), [pivot])

PIVOT is a reserverd word in SQL2005.

(I assume that pivot is a column name?)

HTH!

|||

Yes you are right Thanks a lot

SVGP.

|||

Pls do the following corrections,

Code Snippet

CREATE PROCEDURE crosstab

@.select varchar(8000),

@.sumfunc varchar(100),

@.pivot varchar(100),

@.table varchar(100)

AS

DECLARE @.sql varchar(8000), @.delim varchar(1)

SET NOCOUNT ON

SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @.pivot + ' AS [pivot] INTO ##pivot FROM ' + @.table + ' WHERE 1=2')

EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @.pivot + ' FROM ' + @.table + ' WHERE '

+ @.pivot + ' Is Not Null')

SELECT @.sql='', @.sumfunc=stuff(@.sumfunc, len(@.sumfunc), 1, ' END)' )

SELECT @.delim=CASE Sign( CharIndex('char', data_type)+CharIndex('date', data_type) )

WHEN 0 THEN '' ELSE '''' END

FROM tempdb.information_schema.columns

WHERE table_name='##pivot' AND column_name='pivot'

SELECT @.sql= @.sql + '''' + convert(varchar(100), [pivot]) + ''' = ' +

stuff(@.sumfunc,charindex( '(', @.sumfunc )+1, 0, ' CASE ' + @.pivot + ' WHEN '

+ @.delim + convert(varchar(100), [pivot]) + @.delim + ' THEN ' ) + ', ' FROM ##pivot

DROP TABLE ##pivot

SELECT @.sql=left(@.sql, len(@.sql)-1)

SELECT @.select=stuff(@.select, charindex(' FROM ', @.select)+1, 0, ', ' + @.sql + ' ')

EXEC (@.select)

SET ANSI_WARNINGS ON

Friday, March 9, 2012

please verify syntax

I haven't written any .net database updates before. I wrote the following code for a visual web developer application and it appears to work. There is no close function or anything. Will the adapter, builder, dataset, and row objects be cleaned up and disposed of properly? Please tell me if I need to include anything else. The connection object is made elsewhere. Thanks

Dim adapterAsNew Data.SqlClient.SqlDataAdapter("SELECT * FROM Inventory WHERE InUnique=" & ListValue.Value, connect)

Dim builderAsNew Data.SqlClient.SqlCommandBuilder(adapter)

Dim datasetAs Data.DataSet =New Data.DataSet()

adapter.Fill(dataset,"Inventory")

Dim datarowAs Data.DataRow = dataset.Tables(0).Rows(0)

datarow("InAgUnique") = Val(InAgUnique.Text)

datarow("InTyUnique") = Val(InTyUnique.Text)

datarow("InName") = InName.Text

datarow("InPnUniquePrimary") = Val(InPnUniquePrimary.Text)

datarow("InPnUniqueSecondary") = Val(InPnUniqueSecondary.Text)

datarow("InPnUniqueTertiary") = Val(InPnUniqueTertiary.Text)

datarow("InPnUniqueSupervisor") = Val(InPnUniqueSupervisor.Text)

datarow("InDescription") = InDescription.Text

datarow("InProblemInstructions") = InProblemInstructions.Text

adapter.Update(dataset,"Inventory")

If the connection is not open, Adapter would open it and once done with the operation, would close itself (as per .net 1.1), not sure of the chanegs in 2.0 though!