Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Tuesday, March 20, 2012

Save time in Script Component which is almost forwarding

Hi,
I have to add the fields coming from the source AS IS but, I need to add current date as a column to it. So, What I do is to add an Script Component and add each and every output column in that along with defining their types and writing an "assignments" script.

Is there any possibility for me to save time in the scenarios where I am almost passing on the information to next level in the data flow ?

Any input regarding this will sincerely be appreciated.

FahadWhy can't you use a derived column transformation with getdate() as an expression? Using a derived column, you can add a new date column to your data flow.|||Thanks, I appreciate it.

Save table fields to a text document

Hi. Need to know how to copy all the fields (not the data) of one table to a
text document using Enterprise Manager.
Thanks, alejandro.
...you can find the id of your table in sysObjects, and then "select * from
syscolumns where id=..."
to obtain a list that you can copy and paste to a text file.
Or you can define a DTS transformation between a SQL source (select top 0 *
from TableName) and a text file destination.Than click "Properties" button
in the text file destination and check "First Row has column names" option.
Francesco Anti
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.
|||Asked and answered in .programming -- please refrain from multi-posting.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.
|||thats good, tnx
"Francesco Anti" wrote:

> ...you can find the id of your table in sysObjects, and then "select * from
> syscolumns where id=..."
> to obtain a list that you can copy and paste to a text file.
> Or you can define a DTS transformation between a SQL source (select top 0 *
> from TableName) and a text file destination.Than click "Properties" button
> in the text file destination and check "First Row has column names" option.
> Francesco Anti
> "alejandro" <alejandro@.discussions.microsoft.com> wrote in message
> news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> to a
>
>

Save table fields to a text document

Hi. Need to know how to copy all the fields (not the data) of one table to
a
text document using Enterprise Manager.
Thanks, alejandro....you can find the id of your table in sysObjects, and then "select * from
syscolumns where id=..."
to obtain a list that you can copy and paste to a text file.
Or you can define a DTS transformation between a SQL source (select top 0 *
from TableName) and a text file destination.Than click "Properties" button
in the text file destination and check "First Row has column names" option.
Francesco Anti
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.|||Asked and answered in .programming -- please refrain from multi-posting.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.|||thats good, tnx
"Francesco Anti" wrote:

> ...you can find the id of your table in sysObjects, and then "select * fr
om
> syscolumns where id=..."
> to obtain a list that you can copy and paste to a text file.
> Or you can define a DTS transformation between a SQL source (select top 0
*
> from TableName) and a text file destination.Than click "Properties" button
> in the text file destination and check "First Row has column names" option
.
> Francesco Anti
> "alejandro" <alejandro@.discussions.microsoft.com> wrote in message
> news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> to a
>
>

Save table fields to a text document

Hi. Need to know how to copy all the fields (not the data) of one table to a
text document using Enterprise Manager.
Thanks, alejandro....you can find the id of your table in sysObjects, and then "select * from
syscolumns where id=..."
to obtain a list that you can copy and paste to a text file.
Or you can define a DTS transformation between a SQL source (select top 0 *
from TableName) and a text file destination.Than click "Properties" button
in the text file destination and check "First Row has column names" option.
Francesco Anti
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.|||Asked and answered in .programming -- please refrain from multi-posting.
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"alejandro" <alejandro@.discussions.microsoft.com> wrote in message
news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> Hi. Need to know how to copy all the fields (not the data) of one table
to a
> text document using Enterprise Manager.
> Thanks, alejandro.|||thats good, tnx
"Francesco Anti" wrote:
> ...you can find the id of your table in sysObjects, and then "select * from
> syscolumns where id=..."
> to obtain a list that you can copy and paste to a text file.
> Or you can define a DTS transformation between a SQL source (select top 0 *
> from TableName) and a text file destination.Than click "Properties" button
> in the text file destination and check "First Row has column names" option.
> Francesco Anti
> "alejandro" <alejandro@.discussions.microsoft.com> wrote in message
> news:E5E25601-E0E9-4A2B-8CBB-6C4D2F92AFC8@.microsoft.com...
> > Hi. Need to know how to copy all the fields (not the data) of one table
> to a
> > text document using Enterprise Manager.
> >
> > Thanks, alejandro.
>
>

Monday, March 12, 2012

save boolean values in a sql2000 table

I try to insert VB.NET boolean true values into a SQL2000 table thru a stored procedure. While varchar fields of the table are correctly filled, I'm surprised to find the bit fields all stay 0 after every single insert. This makes me wonder if VB.NET boolean variables can be passed to sp bit parameters and inserted into a SQL2000 table. Or did I miss something? Please advise. Thanks.Oop! I found a bug in the GUI that captures the input. It's fixed and now works fine. Thanks.

Friday, March 9, 2012

save an image into a table using Query Analyzer

I have a table with two fields Part_num and Pic in SQL server 2000
Pic is of Image type. Is there a way I can save images for each part_num using Query analyzer?You can use BULK INSERT or bcp.

From BOL:

xp_cmdshell 'bcp pubs..bitmap in test.doc -Usa -Ppassword -Sservername'

--------------------
The Bcp.fmt file:

8.0
1
1 SQLIMAGE 0 5578 "" 1 c1

Using the BULK INSERT statement to bulk copy the Test.doc data file into the bitmap table in the pubs database, execute from a query tool, such as SQL Query Analyzer:

BULK INSERT pubs..bitmap FROM 'c:\test.doc'
WITH (
FORMATFILE = 'c:\Bcp.fmt'
)|||Sorry I'm new to this, Will this work if I have an image, lets say 150.bmp on C: drive? So would it be
BULK INSERT pubs..bitmap FROM 'c:\150.bmp'
WITH (
FORMATFILE = 'c:\150.bmp'
) ?

Originally posted by snail
You can use BULK INSERT or bcp.

From BOL:

xp_cmdshell 'bcp pubs..bitmap in test.doc -Usa -Ppassword -Sservername'

--------------------
The Bcp.fmt file:

8.0
1
1 SQLIMAGE 0 5578 "" 1 c1

Using the BULK INSERT statement to bulk copy the Test.doc data file into the bitmap table in the pubs database, execute from a query tool, such as SQL Query Analyzer:

BULK INSERT pubs..bitmap FROM 'c:\test.doc'
WITH (
FORMATFILE = 'c:\Bcp.fmt'
)|||Check this (I tested it):

BULK INSERT pictures FROM 'c:\tmp\bridge.jpg'
WITH (
FORMATFILE = 'c:\tmp\bcp.fmt'
)

bcp.fmt
3935 - size of image, you have to change collation if it needs.

See attachment.

Tuesday, February 21, 2012

Sample output

hi, i want to know how to do that:

table: costtypes

fields : typecode

values: amount, vat, duty,

table bills:

fields: billid, costtype, amount

value

bi1 amount 1000

bi1 vat 10

bi2 amount 200

i need to create a view that will display amount, vat, duty as fields. note that these are values in the table.

so that i will have

billid amount vat duty

bi1 1000 10 0

bi2 200 0 0

thanks

-- Create #Bills table and values

Create Table #CostTypes (TypeCode varchar(10))

Go

Insert Into #CostTypes

Select 'Amount'

Union

Select 'Vat'

Union

Select 'Duty'

Go

-- Create #Bills table and values

Create Table #Bills (BillId int, CostType varchar(10), value int)

Go

Insert Into #Bills

Select 1, 'Amount', 1000

Union

Select 1, 'Vat', 10

Union

Select 2, 'Amount', 200

Go

-- Create view (you could use this code in a table function)

Declare @.fieldname varchar(10)

Declare @.stmt varchar(4000)

Declare Fields Cursor For Select TypeCode From #CostTypes

Set @.stmt = 'Select Distinct BillId'

Open Fields

Fetch Next From Fields Into @.fieldname

While @.@.Fetch_Status = 0 Begin

Set @.stmt = @.stmt + ', Value * Case When CostType = ''' + @.fieldname + ''' Then 1 Else 0 End As ' + @.fieldname

Fetch Next From Fields Into @.fieldname

End

Close Fields

Deallocate Fields

Set @.stmt = @.stmt + ' From #Bills'

Exec(@.stmt)

I hope it's useful

Giulio

|||Thats too good . can you explain whats happenin in the code though . only the fetch etc part|||

Declare @.fieldname varchar(10)

Declare @.stmt varchar(4000)

Declare Fields Cursor For Select Amounttype From Amounttypes

Set @.stmt = 'Select Distinct BillId'

Open Fields

Fetch Next From Fields Into @.fieldname

While @.@.Fetch_Status = 0 Begin

Set @.stmt = @.stmt + ', amountfc * Case When Amounttype = ''' + @.fieldname + ''' Then 1 Else 0 End As ' + @.fieldname

Fetch Next From Fields Into @.fieldname

End

Close Fields

Deallocate Fields

Set @.stmt = @.stmt + ' From Bill'

Exec(@.stmt)

|||

billid amount vat duty

699190BD-1410-47A8-AFAD-05EC38FEDBF8 0.00 50.00 0.00
B9B71063-D2AD-4735-8CC0-106335990A93 1.00 0.00 0.00
48A82C3C-CF64-4202-B6FA-5DD5A3D32D3C 0.00 0.00 210.00

|||

Sorry, I forgot table function definition requires you know returned table structure.

You could create a scalar function to get SQL Select statement and use this to get data...

Bye,

Giulio

|||

Declare @.fieldname varchar(10) -- variable used to get field value from AmountTypes table and use it as field name in your view

Declare @.stmt varchar(4000) -- variable that contains sql select statement to get data

Declare Fields Cursor For Select Amounttype From Amounttypes -- cursor to scan AmountTypes records and get view fields list

Set @.stmt = 'Select Distinct BillId' -- initialize @.stmt.

Open Fields -- Open Fields cursor

Fetch Next From Fields Into @.fieldname -- get first record

While @.@.Fetch_Status = 0 Begin -- while reading AmountTypes...

Set @.stmt = @.stmt + ', amountfc * Case When Amounttype = ''' + @.fieldname + ''' Then 1 Else 0 End As ' + @.fieldname -- ...add field to select fields list

Fetch Next From Fields Into @.fieldname -- read next record

End

Close Fields -- close Fields cursor

Deallocate Fields -- remove Fields cursor

Set @.stmt = @.stmt + ' From Bill' -- add FROM clause to select statement

Exec(@.stmt) -- execute select

At the end of code below, your select statement should be something like

Select Distinct BillId, amountfc * Case When AmountType = 'Amount' Then 1 Else 0 End As Amount, amountfc * Case When AmountType = 'Vat' Then 1 Else 0 End As Vat, amountfc * Case When AmountType = 'Duty' Then 1 Else 0 End As Duty From Bill

Simply I read AmountTypes to get fields list. Since you have AmountType in Bill as value, I multiply amountfc by 1 if AmountType is equal to current cursor's AmountType or by 0 else.

Ok?

|||

ca101 AD2574DB Estimated 0.00 0.00 2100.00
ca101 AD2574DB Estimated 3000.00 0.00 0.00
ca101 7BB1C98B Cheque 0.00 1.00 0.00
ca101 18ACADF5 Cheque 0.00 0.00 500.00
ca101 18ACADF5 Cheque 0.00 1100.00 0.00
nmh500 AD2574DB Estimated 0.00 0.00 10.00
nmh500 AD2574DB Estimated 2000.00 0.00 0.00

okay if u see the rows., those in same colour should have been on the same row, so

ca101 AD2574DB Estimated 3000.00 0.00 2100.00

code i used follows;

Declare @.fieldname varchar(10)

Declare @.stmt varchar(4000)

Declare Fields Cursor For Select Amounttype From Amounttypes

Set @.stmt = 'Select pono, myid, billtype'

Open Fields

Fetch Next From Fields Into @.fieldname

While @.@.Fetch_Status = 0 Begin

Set @.stmt = @.stmt + ', amountfc * Case When Amounttype = ''' + @.fieldname + ''' Then 1 Else 0 End As ' + @.fieldname

Fetch Next From Fields Into @.fieldname

End

Close Fields

Deallocate Fields

Set @.stmt = @.stmt + ' From multiplebillsviewall'

Exec(@.stmt)

|||

sql never got so interesting in my life.

i know mere basics u know thanks millions.

|||

Sorry, I forgot it...

Set @.stmt = @.stmt + ', Sum(Value * Case When CostType = ''' + @.fieldname + ''' Then 1 Else 0 End) As ' + @.fieldname

and

Set @.stmt = @.stmt + ' From #Bills Group By BillId '

it should be ok...

|||

ca101 AD2574DB-69C0-496F-AD82-67B87AEF2052 Estimated 3000.00 0.00 2100.00
ca101 7BB1C98B-923B-471D-AC86-9F331671F789 Cheque 0.00 1.00 0.00
ca101 18ACADF5-E7D0-4B97-B79D-E266F07C804D Cheque 0.00 1100.00 500.00
nmh500 AD2574DB-69C0-496F-AD82-67B87AEF2052 Estimated 2000.00 0.00 10.00

lovely bra.

thanks.

by the way how do i create a view out of this now?

i will simply use the view in .net and display .

|||

okay no problemo i created a stored procedure.

views cannot contain declare, set etc..

but if the possibility that i can create a view out of this exists . let me know.

shot bra SQL ROCKS

|||

I don't think this possibility exists...

|||so whats the next proposal .. i only need to be able to bring the data to my application in .net and display it in a datagridview.|||

you could simply execute a select statement got by calling a scalar function that returns it...

Create Function dbo.xGetAmountSelectStmt()
Returns Varchar(4000)
As Begin
Declare @.fieldname varchar(10)
Declare @.stmt varchar(4000)
Declare Fields Cursor For Select AmountType From AmountTypes

Set @.stmt = 'Select BillId'

Open Fields
Fetch Next From Fields Into @.fieldname

While @.@.Fetch_Status = 0 Begin
Set @.stmt = @.stmt + ', Sum(Value * Case When AmountType = ''' + @.fieldname + ''' Then 1 Else 0 End) As ' + @.fieldname
Fetch Next From Fields Into @.fieldname
End

Close Fields
Deallocate Fields

Set @.stmt = @.stmt + ' From Bills Group By BillId '
Return(@.stmt)
End

When you have to get data, get below function return value and use it as select statement...

If you need to inject fields, join, where etc you could may simply modify last Set @.stmt as

Set @.stmt = @.stmt + '/*Select*/ From Bills /*Joins*/ /*Where*/ Group By BillId /*Group*/ /*Order*/'

and replace these optional pseudo-tags as your needs

/*Select*/ -> Other fields to get using joins or as static values beginning with ","

/*Joins*/ -> Table joins

/*Where*/ -> Where clause

/*Group*/ -> Other fields used in Group By beginning with ","

/*Order*/ -> Order By clause

Since they are delimited by /*..*/ if you don't need to use tags below, you may leave they as are

I think this could be better (more readable, simple and clean) than execute all every time... And more, if you have to change anything in function code, you only have to modify server-side function...