Showing posts with label names. Show all posts
Showing posts with label names. Show all posts

Wednesday, March 21, 2012

How to create a table with query

I want to create a empty new table (if it doesn't exist)
when the the page/program starts. Maybe with copying field names from other table.
It would be nice to have its name personal for each user. But I don't know if it possible.
Also, what event is best place for this.

Perhaps like this:

dim username as string = Page.User.Identity.Name

Dim tablename as string

tablename= shopping_basket+username

create from t_product new

@.tablename

What you are thinking of doing is not a very good idea. You will have to give the user higher permissions to create tables on the fly and if they are not cleaned up properly you will end up with tons of tables that you dont know if they are being used or not. On the otherhand, think of using temp tables or table variables in the proc.

|||

Aha good to know, and yes I should thought about that myself.

What about a table with a constant name. But then, have you good code using or making a temp table or table variable.

What is a table variable?

Regards

Leif

|||

LISM:

What about a table with a constant name.

You can do that too. If you can identify a batch from a different batch via some unique key. If you have multiple users inserting into that table you should have a way of identifying one set from the other.

LISM:

But then, have you good code using or making a temp table or table variable.

What is a table variable?

Please read up books online on these topics. They have a much better explanation than what I can type.

Friday, March 9, 2012

How to create a column names using results of query?

I need to generate a report where the column names are defined by the result
s
of the first query and the data by the second query.
The closest I can get is to create a stored procedure that creates a temp
table with named columns fill it with a single row (query 1) and then fill i
t
with data. I then query the table for my results to pass on to the
application dropping the table when done. My issue with this method is that
App I am passing data to shows the temp column names as well (no way to
suppress them).
Over simplified pseudo code:
CREATE TABLE ##c (
ordr int,
Col1 varchar(30),
Col2 varchar(30)
)
INSERT INTO ##c
SELECT ‘1’ AS [ordr], h.header1, h.header2
FROM dbo.configure h
WHERE h.id = ‘105’
INSERT INTO ##c
SELECT ‘2’ AS [ordr], d.data1, d.data2
FROM dbo.detail d
WHERE d.id = ‘105’
ORDER by d.data1
SELECT col1, col2
FROM ##c
ORDER BY ordr, col1, col2
DROP TABLE ##cYou could generate a SELECT string you need with column names from the first
query and rename them use the AS keyword.
Then you can use EXECUTE (T-SQL command) to execute it.
Search the BOL for "Using EXECUTE with a Character String"
Milan
"Dan" <Dan@.discussions.microsoft.com> wrote in message
news:B43F7817-1C89-44AF-A4EE-15FCC0F17B52@.microsoft.com...
>I need to generate a report where the column names are defined by the
>results
> of the first query and the data by the second query.
> The closest I can get is to create a stored procedure that creates a temp
> table with named columns fill it with a single row (query 1) and then fill
> it
> with data. I then query the table for my results to pass on to the
> application dropping the table when done. My issue with this method is
> that
> App I am passing data to shows the temp column names as well (no way to
> suppress them).
> Over simplified pseudo code:
> CREATE TABLE ##c (
> ordr int,
> Col1 varchar(30),
> Col2 varchar(30)
> )
> INSERT INTO ##c
> SELECT '1' AS [ordr], h.header1, h.header2
> FROM dbo.configure h
> WHERE h.id = '105'
> INSERT INTO ##c
> SELECT '2' AS [ordr], d.data1, d.data2
> FROM dbo.detail d
> WHERE d.id = '105'
> ORDER by d.data1
> SELECT col1, col2
> FROM ##c
> ORDER BY ordr, col1, col2
> DROP TABLE ##c
>|||You can do something like the following:
IF OBJECT_ID (N'teble1') IS NOT NULL
DROP FUNCTION table1
GO
select * into table1 from Query1 where 0 = 1
insert into table1 select * from Query2
"Dan" <Dan@.discussions.microsoft.com> wrote in message news:B43F7817-1C89-44AF-A4EE-15FCC0F
17B52@.microsoft.com...
>I need to generate a report where the column names are defined by the resul
ts
> of the first query and the data by the second query.
> The closest I can get is to create a stored procedure that creates a temp
> table with named columns fill it with a single row (query 1) and then fill
it
> with data. I then query the table for my results to pass on to the
> application dropping the table when done. My issue with this method is tha
t
> App I am passing data to shows the temp column names as well (no way to
> suppress them).
> Over simplified pseudo code:
> CREATE TABLE ##c (
> ordr int,
> Col1 varchar(30),
> Col2 varchar(30)
> )
> INSERT INTO ##c
> SELECT '1' AS [ordr], h.header1, h.header2
> FROM dbo.configure h
> WHERE h.id = '105'
> INSERT INTO ##c
> SELECT '2' AS [ordr], d.data1, d.data2
> FROM dbo.detail d
> WHERE d.id = '105'
> ORDER by d.data1
> SELECT col1, col2
> FROM ##c
> ORDER BY ordr, col1, col2
> DROP TABLE ##c
>|||Do you mean:
Declare @.c1 as varchar(30)
Set @.c1 = Select col1 from table1
Select newCol1 AS @.c1 from table2
if so I can not get that to work
"Milan Kosanovic" wrote:

> You could generate a SELECT string you need with column names from the fir
st
> query and rename them use the AS keyword.
> Then you can use EXECUTE (T-SQL command) to execute it.
> Search the BOL for "Using EXECUTE with a Character String"
> Milan
> "Dan" <Dan@.discussions.microsoft.com> wrote in message
> news:B43F7817-1C89-44AF-A4EE-15FCC0F17B52@.microsoft.com...
>
>|||I am missing somthing hear as well:
I can not get my query clauses to run within the framework laid out.
"fish" wrote:

> You can do something like the following:
> IF OBJECT_ID (N'teble1') IS NOT NULL
> DROP FUNCTION table1
> GO
> select * into table1 from Query1 where 0 = 1
> insert into table1 select * from Query2
>
> "Dan" <Dan@.discussions.microsoft.com> wrote in message news:B43F7817-1C89-
44AF-A4EE-15FCC0F17B52@.microsoft.com...
>
>|||Have you ever considered being a good programmer who does his reports
in the front end instead of writing kludges in the database? This is
the whole idea of a tiered architecture.
Have you ever thought about writing declarative code instead mimicking
the steps of a procedural program? You are even trying to name a table
the way we used to name a tape on a mag tape system.|||> Have you ever considered being a good programmer who does his reports
> in the front end instead of writing kludges in the database? This is
> the whole idea of a tiered architecture.
IMHO report writing is a simple and boring task for 95% of time. IMHO
using a good programmer for report writing is an overkill - in my
recent experience a high scholl senior quickly mastered Crystal Reports
and
did all that booooooooring label aligning pretty well, and for a cheap
price. Overall it was a faster and cheaper approach as opposed to the
traditional one...|||Requestor
Character string
Trust me this is not my preferred method but the client’s dictated report
application and the predefined legacy applications db structure are off
limits to me. And I have this one obstacle to getting sign off. My boundarie
s
are that the I can pass a string to the db and the grid returned is what the
user see’s
I have figured out that I can
declare @.col1 as Varchar(15)
declare @.sql as varchar(755)
set @.col1 = (select some stuff)
Set @.sql = 'create table#t ([' + @.col1 + ] varchar(30))'
exec (@.sql)
not great but it gets me out the door within the clients constraints
"Alexander Kuznetsov" wrote:

> IMHO report writing is a simple and boring task for 95% of time. IMHO
> using a good programmer for report writing is an overkill - in my
> recent experience a high scholl senior quickly mastered Crystal Reports
> and
> did all that booooooooring label aligning pretty well, and for a cheap
> price. Overall it was a faster and cheaper approach as opposed to the
> traditional one...
>

Wednesday, March 7, 2012

How to count existing items?

I want to check and see if an item exists within one of my tables
before I go and replicate. let's say that "John Doe" already exists in
a names database and the user tries to add him again, i want to notify
the user that this name already exists. also, i want to do this
programatically(sp?) in C#.
any help?Put a unique constraint on the identifying columns. When you try to
insert (using a stored procedure), SQL Server will throw an error
(2627). Handle the error in your C# app.
JLuv wrote:
> I want to check and see if an item exists within one of my tables
> before I go and replicate. let's say that "John Doe" already exists in
> a names database and the user tries to add him again, i want to notify
> the user that this name already exists. also, i want to do this
> programatically(sp?) in C#.
> any help?|||You could do this in this manner
USE Northwind
GO
IF NOT EXISTS
( SELECT (LastName)
FROM Employees
WHERE ( LastName = 'Fuller'
AND FirstName = 'Andrea'
)
) INSERT INTO Employees
( LastName
, FirstName
)
VALUES
( 'Fuller'
, 'Andrea'
)
Your application can check the RowCount (RowsAffected) to determine if more
0 (zero) rows were inserted. Then message the user appropriately.
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"JLuv" <JLuv3k6@.gmail.com> wrote in message news:1151692692.332535.294860@.75g2000cwc.google
groups.com...
>I want to check and see if an item exists within one of my tables
> before I go and replicate. let's say that "John Doe" already exists in
> a names database and the user tries to add him again, i want to notify
> the user that this name already exists. also, i want to do this
> programatically(sp?) in C#.
> any help?
>|||i've never tried that before. is this method called a "unique
constraint"? what should i search for to find information on this?
Stu wrote:
> Put a unique constraint on the identifying columns. When you try to
> insert (using a stored procedure), SQL Server will throw an error
> (2627). Handle the error in your C# app.
>
> JLuv wrote:|||i did something like that. i went and updated the database with the
exact same information it already holds. it returns the correct # of
columns affected using ExecuteNonQuery().
Arnie Rowland wrote:
> You could do this in this manner
> USE Northwind
> GO
> IF NOT EXISTS
> ( SELECT (LastName)
> FROM Employees
> WHERE ( LastName = 'Fuller'
> AND FirstName = 'Andrea'
> )
> ) INSERT INTO Employees
> ( LastName
> , FirstName
> )
> VALUES
> ( 'Fuller'
> , 'Andrea'
> )
> Your application can check the RowCount (RowsAffected) to determine if mor
e 0 (zero) rows were inserted. Then message the user appropriately.
>
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "JLuv" <JLuv3k6@.gmail.com> wrote in message news:1151692692.332535.294860@.
75g2000cwc.googlegroups.com...|||You could also use the 'Primary Key' -it is by design a 'unique constraint'.
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"JLuv" <JLuv3k6@.gmail.com> wrote in message
news:1151694549.094788.110810@.h44g2000cwa.googlegroups.com...
> i've never tried that before. is this method called a "unique
> constraint"? what should i search for to find information on this?
>
> Stu wrote:
>|||Not columns affected -BUT Rows Affected. Notice the use of IF NOT EXISTS.
That prevents adding a row that meets the WHERE clause criteria.
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"JLuv" <JLuv3k6@.gmail.com> wrote in message
news:1151694709.234122.133440@.75g2000cwc.googlegroups.com...
>i did something like that. i went and updated the database with the
> exact same information it already holds. it returns the correct # of
> columns affected using ExecuteNonQuery().
>
> Arnie Rowland wrote:
>|||yea, i mean row, not column. and i'll try out IF NOT EXIST
Arnie Rowland wrote:
> Not columns affected -BUT Rows Affected. Notice the use of IF NOT EXISTS.
> That prevents adding a row that meets the WHERE clause criteria.
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "JLuv" <JLuv3k6@.gmail.com> wrote in message
> news:1151694709.234122.133440@.75g2000cwc.googlegroups.com...|||The only real reason to use this 'IF NOT EXISTS' form is if you wish to have
an ELSE -such as update an existing record.
Otherwise, just insert the data and let the unique constraint or Primary key
force an error and your application can capture the error and react
accordingly.
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"JLuv" <JLuv3k6@.gmail.com> wrote in message
news:1151695591.304350.187130@.i40g2000cwc.googlegroups.com...
> yea, i mean row, not column. and i'll try out IF NOT EXIST
>
> Arnie Rowland wrote:
>