|
No matter how you work it you are not going to get what you want.
Select * from @TableName
The sames as this will not work
Select @ColumnName from Table
You are going to have to construct a string and execute it, it is called dynamic SQL and this is one of the few reason it is a valid solution.
BTW
I agree that your db looks ugly (I presume you are aware of that). Have you looked into partitioning, as I assume you have split the orders by country/date for performance reasons.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks All for your help, and i will find an alternative way to do it then.
|
|
|
|
|
hello
you can use the exec function.
like this:
exec('select * from ' + @TableName)
that execute a query and return the result set
good luck
|
|
|
|
|
your alternatives are limited
1. create seperate SQL queries for all possibilities
2. Create an external application to overcome it
3. use dynamic SQL, Erlands Dynamic SQL article
others might chip in other alternatives for you
As barmey as a sack of badgers
|
|
|
|
|
You will have to use dynamic queries or a series of if/else blocks to do this. Dynamic queries are not a good way to do things. If/else, will look ugly if you have even 10 of them. So, either live with dynamic queries or redesign things.
|
|
|
|
|
Please look into below MDX query it is resulting in system.outofmemoryexception when i try to run in MDX query analyzer
Details
MRCODE has 1797 rows
DIV has 6 rows
STKCD has 4434 rows
Product Name has 507 rows
when crossjoin this .....it is giving problems..
Please let me know in case any modifications on it i cannot try NONEMPTYCROSSJOIN on it
SELECT ALL ( Hierarchize(
CrossJoin([Dim SM].[MRCODE].[MRCODE].members,
CrossJoin([Dim SM].[DIV].[DIV].members,
CrossJoin(
[Dim SM].[STKCD].[STKCD].members,
[D Im Prod].[Product Name].members
)))))
ON ROWS,[Measures].[QTYCHRG]ON COLUMNS from [ABCDW2]
thanks in advance
|
|
|
|
|
Hi All,
I'm trying to create a table that has one primary key with multiple foreign keys which reference different tables. The following is how I declared my table:
CREATE TABLE COURSE_SECTION
(
Csecid NUMBER(8),
Secnum NUMBER(2) NOT NULL,
Maxenrl NUMBER(4) NOT NULL,
Currenrl NUMBER(4) NOT NULL,
Cid NUMBER(6) NOT NULL,
Termid NUMBER(5) NOT NULL,
Day VARCHAR2(10),
Locid NUMBER(5),
Fid NUMBER(4),
primary Key (Csecid),
foreign Key (Cid) references COURSE(Cid),
foreign Key (Termid) references TERM(Termid),
foreign Key (Locid) references LOCATION(Locid),
foreign Key (Fid) references FACULTY(Fid)
);
I've receive the error message "unknown command beginning ........ - rest of line ignored" for many of the lines when I execute the above script in Oracle SQL Plus. I have tried many different things like rewording the lines or capitalize the letters of the attributes but my attempts were futile. Can you please point out what I have done wrong? Thank you in advance for your help.
|
|
|
|
|
Hi,
How can I create a foreign key column which is referencing the same tables another column. And if I have already created the table then how can I alter the table to enforce the same.
Any link or any sort of help will be very much usefull.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
|
I got the answer just create in normal way how we create table in SQL Server.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|
|
Exactly. A very handy technique.
|
|
|
|
|
Learn something new every day.
|
|
|
|
|
when we use OPENXML then we have to mention table structure like below one.
DECLARE @xml_text VARCHAR(4000), @i INT
/* put xml structure here */
EXEC sp_xml_preparedocument @i OUTPUT, @xml_text
SELECT au_id AS author_id,
au_lname AS last_name,
au_fname AS first_name,
a.title_id,
title,
royaltyper AS royalty
FROM
OPENXML(@i, '/root/authors/titles', 1)
WITH (
au_id VARCHAR(11),
au_lname VARCHAR(20) '../@au_lname',
au_fname VARCHAR(30) '../@au_fname',
title_id VARCHAR(15),
royaltyper INT) a
INNER JOIN titles b ON a.title_id = b.title_id
WITH (
au_id VARCHAR(11),
au_lname VARCHAR(20) '../@au_lname',
au_fname VARCHAR(30) '../@au_fname',
title_id VARCHAR(15),
royaltyper INT
)
suppose if i dont want to mention the table structure manually or hard coded rather dynamically fetch table structure,datatype and length then how could i proceed.
please help me with sample code.
thanks in advance
tbhattacharjee
|
|
|
|
|
Hallo,
I have a database copy of a live database for development. On the live database is running a replication, but in the dev database it is not. However there are replication contraints and trigger in the dev database, and I can`t, for example, add columns to tables.
How can I delete all this replication artefacts?
Thank you for your help!
|
|
|
|
|
|
I have select query and I am getting results like
2
4
6
If I Pass 6 as Parameter then i need to get previous number of 6 i,e 4. If 9 then it has to give 0.
How can I write the query for this in Sql Server 2005
|
|
|
|
|
DECLARE
@ID INT
SET @ID = 4
SELECT TOP 1 ID
FROM tablename
WHERE ID < @ID
ORDER BY ID DESC
Test the result for null and replace with 0
OR
Put the results into a variable in the procedure and test the variable and return the results.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello there...
I have a query which spends a lot of time calculating my CASE WHEN -statements.
There are around 16 CASE WHEN statements, which takes a huge time to calculate.
Is there any way to rewrite query?
My query looks like this
SELECT DISTINCT Product_Master.Product_Kid, Product_Master.Product_Name, convert nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate,
CASE WHEN (Product_Master.Product_Code = '' OR Product_Master.Product_Code IS NULL) THEN '-' ELSE Product_Master.Product_Code END AS Product_Code,
CASE WHEN (Product_Master.Product_ModelNo = '' OR Product_Master.Product_ModelNo IS NULL) THEN '-' ELSE Product_Master.Product_ModelNo END AS Product_ModelNo,
CASE WHEN (Product_Master.Product_Image = '' OR Product_Master.Product_Image IS NULL) THEN 'N' ELSE 'Y' END AS Product_Image,
CASE WHEN (Product_Master.Product_BrandId = '' OR Product_Master.Product_BrandId IS NULL) THEN 'N' ELSE 'Y' END AS Brand_Name FROM Product_Master ORDER BY RegDate DESC
|
|
|
|
|
case when (isnull(Product_Master.Product_Code, '') = '' then '-' else Product_Master.Product_Code end as Product_Code
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Personally, I find it hard to believe that the CASE statements are what's taking time since they don't look complex. How do you know that the CASE statements are what take the most time? Have you tried the same query without some of the CASE statements, just returning the actual column data instead, and comparing the performance? Is the DISTINCT required? Is RegDate indexed?
Scott
|
|
|
|
|
Thanks for reply Scott,
Yes, I have tried the same query without CASE WHEN statements, and it works faster comperatively.
Moreover, The query is not this much only, there are many more CASE WHEN and Left Joins(which are necessary).
Left Joins are essential for the result, so I can't move it. The only options is to find alternate for CASE WHEN statements.
Prachi
|
|
|
|
|
Then we'd probably have to see the whole query since the little we've seen doesn't indicate a problem. It would help to know the table structures and rough row counts.
Scott
|
|
|
|
|
Scott,
here is the whole query.
and roughly, records for each supplier is minimum 400(rows)
declare @a as char(1)
declare @b as varchar(35)
set @a='Y'
set @b='<span style=''color:Red''>N</span>'
SELECT DISTINCT
Product_Master.Product_Kid, Product_Master.Product_Name, convert(nvarchar,Product_Master.Product_RegDate,103) as Product_RegDate, Product_Master.Product_RegDate as RegDate,
Isnull(Product_Master.Product_Code,'-') AS Product_Code,
Isnull(Product_Master.Product_ModelNo,'-') AS Product_ModelNo,
CASE WHEN (Product_Master.Product_Image IS NULL) THEN @b ELSE @a END AS Product_Image,
CASE WHEN (Product_Master.Product_BrandId IS NULL) THEN @b ELSE @a END AS Brand_Name,
CASE WHEN (Product_Master.Product_MarketPrice IS NULL) THEN @b ELSE @a END AS Product_MarketPrice,
CASE WHEN (Product_Master.Product_PackagingCharge IS NULL) THEN @b ELSE @a END AS Product_PackagingCharge,
CASE WHEN (Product_Master.Product_MinOrderQty IS NULL) THEN @b ELSE @a END AS Product_MinOrderQty,
CASE WHEN (Product_Master.Product_ShippingQty IS NULL) THEN @b ELSE @a END AS Product_ShippingQty,
CASE WHEN (Product_Master.Product_Weight IS NULL) THEN @b ELSE @a END AS Product_Weight,
CASE WHEN (Product_Master.Product_Dimension IS NULL) THEN @b ELSE @a END AS Product_Dimension,
CASE WHEN (Product_Master.Product_DeliveryPeriod IS NULL) THEN @b ELSE @a END AS Product_DeliveryPeriod,
CASE WHEN (Product_Master.Product_Description Is NULL) THEN @b ELSE @a END AS Product_Description,
CASE WHEN (Product_Master.Product_PcsPerKg IS NULL) THEN @b ELSE @a END AS Product_PcsPerKg,
CASE WHEN (ProductApplication.ProdApp_Code IS NULL) THEN @b ELSE @a END AS ProdApp_Code,
CASE WHEN (ProductFeatures.ProdFeatures_Code IS NULL) THEN @b ELSE @a END AS ProdFeatures_Code,
CASE WHEN (ProductTechnicalSpecification.TechSpec_code IS NULL) THEN @b ELSE @a END AS TechSpec_code,
CASE WHEN (ProductSpecialNotesInstruction.ProdSpecInstr_Code IS NULL) THEN @b ELSE @a END AS ProdSpecInstr_Code,
CASE WHEN (MaterialSafetyDataSheet.MSDS_Code IS NULL) THEN @b ELSE @a END AS MSDS_Code,
CASE WHEN (ProductStandardApproval.ProdStdApproval_Code IS NULL) THEN @b ELSE @a END AS ProdStdApproval_Code,
CASE WHEN (InstallationManual.InstCommManual_Code IS NULL) THEN @b ELSE @a END AS InstCommManual_Code,
CASE WHEN (ProductPackaging.ProdPackaging_Code IS NULL) THEN @b ELSE @a END AS Product_PackagingCode,
CASE WHEN (ProductInstallationCommissioning.InstallationComm_Code IS NULL) THEN @b ELSE @a END AS InstallationComm_Code,
CASE WHEN (Catalog.Catalog_code IS NULL) THEN @b ELSE @a END AS Catalog_code,
CASE WHEN (Product_GuarantyWarrantydetails.gwd_Code IS NULL) THEN @b ELSE @a END AS gwd_Code,
CASE WHEN (OtherDetail_SpareParts.ODTSP_code IS NULL) THEN @b ELSE @a END AS Spares_Code,
CASE WHEN (OtherDetail_IncludedAccessories.ODTIA_code IS NULL) THEN @b ELSE @a END AS AccessoriesIncluded_Code,
CASE WHEN (OtherDetail_OptionalAccessories.ODTOA_code IS NULL) THEN @b ELSE @a END AS AccessoriesOptional_Code,
CASE WHEN (WearAndTearParts.WearAndTearPart_Code IS NULL) THEN @b ELSE @a END AS WearAndTearPart_Code,
CASE WHEN (Consumables.c_code IS NULL) THEN @b ELSE @a END AS C_Code
FROM Product_Master LEFT JOIN
WearAndTearParts ON Product_Master.Product_Kid = WearAndTearParts.WearAndTearPart_ProductId LEFT JOIN
OtherDetail_OptionalAccessories ON Product_Master.Product_Kid = OtherDetail_OptionalAccessories.ODTOA_ProductId LEFT JOIN
OtherDetail_IncludedAccessories ON Product_Master.Product_Kid = OtherDetail_IncludedAccessories.ODTIA_ProductId LEFT JOIN
OtherDetail_SpareParts ON Product_Master.Product_Kid = OtherDetail_SpareParts.ODTSP_ProductId LEFT JOIN
ProductInstallationCommissioning ON Product_Master.Product_Kid = ProductInstallationCommissioning.InstallationComm_ProductId LEFT JOIN
ProductPackaging ON Product_Master.Product_Kid = ProductPackaging.ProdPackaging_ProductId LEFT JOIN
InstallationManual ON Product_Master.Product_Kid = InstallationManual.InstCommManual_ProductId LEFT JOIN
ProductStandardApproval ON Product_Master.Product_Kid = ProductStandardApproval.ProdStdApproval_ProductId LEFT JOIN
MaterialSafetyDataSheet ON Product_Master.Product_Kid = MaterialSafetyDataSheet.MSDS_ProductId LEFT JOIN
ProductSpecialNotesInstruction ON Product_Master.Product_Kid = ProductSpecialNotesInstruction.ProdSpecInstr_ProductId LEFT JOIN
ProductTechnicalSpecification ON Product_Master.Product_Kid = ProductTechnicalSpecification.TechSpec_ProductId LEFT JOIN
Product_GuarantyWarrantydetails ON Product_Master.Product_Kid = Product_GuarantyWarrantydetails.gwd_ProductId LEFT JOIN
ProductFeatures ON Product_Master.Product_Kid = ProductFeatures.ProdFeatures_ProductId LEFT JOIN
ProductApplication ON Product_Master.Product_Kid = ProductApplication.ProdApp_ProductId LEFT JOIN
[Catalog] ON Product_Master.Product_Kid = Catalog.Catalog_productid LEFT JOIN
Consumables ON Product_Master.Product_Kid = Consumables.c_productid
WHERE Product_Master.Product_Isdeleted = '0'
AND Product_Master.Product_SupplierId = '00000273V'
ORDER BY RegDate DESC
select * from supplier_master
|
|
|
|
|
Left joins.....
yup that's costly
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Yeah, what they say. Plus why convert (and transmit) the textual RegDate? Perform that outside the database.
|
|
|
|
|