Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Wednesday, March 28, 2012

question about query part of a string

I'm passing a variable to SQL and I want it to query a column (IP_user), but query any part of what is given. For example if I given Chris it would bring up Chris, Christian, Christine, etc. What is the syntax to do this? thanks

Try this

ALTER PROCEDURE [dbo].[SPU_XXXX]

@.SEARCH VARCHAR(30) = NULL

AS

SET @.SEARCH = '%'+ @.SEARCH + '%'

SELECT CLIENT.CLIENT, CLIENT.CLIENTID

FROM CLIENT

WHERE (CLIENT.CLIENT LIKE @.SEARCH)

|||

The best reference for this question is here:

http://www.sommarskog.se/arrays-in-sql.html

The Table of Numbers method is the easiest, fastest, etc. It will put the data into a table format that you can join to your table in a very straightforward fashion.

http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum

|||

Code Snippet

Set @.search = 'Chris'

--To get all names start with Chris

--i.e., Chrisxxxxxxxx

select * from yourtable where ip_user like @.search + '%'

--To get all names ends with Chris

--i.e., xxxxxChris

select * from yourtable where ip_user like '%' + @.search

--To get all names containes Chris at anyplace

--i.e., xxxxxxChrisxxxxxxxx or xxxxxChris or Chrisxxxxxxxx

select * from yourtable where ip_user like '%' + @.search + '%'

|||

You really should look into Fulltext, as that is its strong feature. Another option using sql engine is to use charindex() function.

e.g.

select *

from tb

where charindex(@.var,your_column)>0

|||

Thank you that was very helpful and it worked great. I have another quick question, and although this isn't technically an access forum it involves access. I have someone who runs access and with an odbc connection, connects to the sql server database. Is there any sql that can be used in accesses queries to replicate this? I apologize ahead of time for posting this here, I couldn't find an access forum...

|||In Access, mark the Query as a 'Pass Though' query, and the query as above will be sent to execute on SQL Server without Access attempting to check syntax and compile.

Monday, March 26, 2012

Question about OUTPUT clause

In the past when inserting a record into a table with an identity column, to return the new identity value I used one of SCOPE_IDENTITY, IDENT_CURRENT, and @.@.IDENTITY.

Question: will this sql 2005 approach also provide the newly added identity value?

insert into TestTable -- ID column of testtable is an Identity field

output inserted.id, inserted.col1 into @.insertedRecords

values('row 20')

select ID from @.insertedRecords

TIA,

Barkingdog

.

Sure can. Did your attempts not work? Here is a script to demo:

create table testTable
(
testTableId int identity primary key,
value varchar(10) unique
)
go

declare @.insertedRows table (
testTableId int primary key,
value varchar(10) unique
)

insert into testTable (value) -- ID column of testtable is an Identity field
output inserted.testTableId, inserted.value into @.insertedRows
values('row 20')

select scope_identity()
select * from @.insertedRows

go

declare @.insertedRows table (
testTableId int primary key,
value varchar(10) unique
)

insert into testTable (value) -- ID column of testtable is an Identity field
output inserted.testTableId, inserted.value into @.insertedRows
select 'row 21'
union all
select 'row 22'

select * from @.insertedRows

|||

I haven't encoutnered any problems but I was just wondering if my original thought was correct.

Thanks.

Barkingdog

sql

Friday, March 23, 2012

question about numeric type

Hi,
I am kind of confused regarding the numeric data type. If I define a numeric
column to be precision 38 (the maximum allowed), in EM the column length
automatically turns to be 17. What's the relationship between 38 and 17?
ThanksThose are storage bytes, lookup decimal and numeric in BOL
Precision Storage bytes
1 - 9 5
10-19 9
20-28 13
29-38 17
Denis the SQL Menace
http://sqlservercode.blogspot.com/
Jen wrote:
> Hi,
> I am kind of confused regarding the numeric data type. If I define a numer
ic
> column to be precision 38 (the maximum allowed), in EM the column length
> automatically turns to be 17. What's the relationship between 38 and 17?
> Thanks|||what is Storage bytes? Thanks
"SQL Menace" wrote:

> Those are storage bytes, lookup decimal and numeric in BOL
> Precision Storage bytes
> 1 - 9 5
> 10-19 9
> 20-28 13
> 29-38 17
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
> Jen wrote:
>|||How many bytes on disk a value of that datatype requires.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...[vbcol=seagreen]
> what is Storage bytes? Thanks
> "SQL Menace" wrote:
>|||Is it true that if the length is 17, then the minimum digits wil be 29 and
the maximum digits will be 38? Thanks
"Tibor Karaszi" wrote:

> How many bytes on disk a value of that datatype requires.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Jen" <Jen@.discussions.microsoft.com> wrote in message
> news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
>|||No, that is now how it work. Compare to int vs. smallint, for instance. You
would be saying that an
int cannot hold numbers lower than some 32767? That is not the case. A large
r storage area can hold
a larger *span* of values. We are not really talking about digits, we are ta
lking about the values
that these digits represent. The values can be represented as binary, 10 bas
e decimal or
hexadecimal, us humans tend to prefer a 10 base decimal representation.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:622544A1-7A46-434A-A539-82D3C5052A52@.microsoft.com...[vbcol=seagreen]
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:
>|||Jen,
If you define a column as decimal/numeric with size 29, then each value
will use 17 bytes of disk space. A column defined as decimal(29,0) can
hold the values -99999999999999999999999999999 thru
99999999999999999999999999999.
If you define the column as decimal(30,0), you can store even bigger
numbers (one decimal digit more). However, each value will still use
(only) 17 bytes of disk space. The same is true for a decimal(38,0).
If you define the column as decimal(28,0), you can 'only' store numbers
between -9999999999999999999999999999 and 9999999999999999999999999999.
However, for the values of this column only 13 bytes of disk space are
needed per value.
HTH,
Gert-Jan
Jen wrote:[vbcol=seagreen]
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:
>

question about numeric type

Hi,
I am kind of confused regarding the numeric data type. If I define a numeric
column to be precision 38 (the maximum allowed), in EM the column length
automatically turns to be 17. What's the relationship between 38 and 17?
ThanksThose are storage bytes, lookup decimal and numeric in BOL
Precision Storage bytes
1 - 9 5
10-19 9
20-28 13
29-38 17
Denis the SQL Menace
http://sqlservercode.blogspot.com/
Jen wrote:
> Hi,
> I am kind of confused regarding the numeric data type. If I define a numeric
> column to be precision 38 (the maximum allowed), in EM the column length
> automatically turns to be 17. What's the relationship between 38 and 17?
> Thanks|||what is Storage bytes? Thanks
"SQL Menace" wrote:
> Those are storage bytes, lookup decimal and numeric in BOL
> Precision Storage bytes
> 1 - 9 5
> 10-19 9
> 20-28 13
> 29-38 17
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
> Jen wrote:
> > Hi,
> >
> > I am kind of confused regarding the numeric data type. If I define a numeric
> > column to be precision 38 (the maximum allowed), in EM the column length
> > automatically turns to be 17. What's the relationship between 38 and 17?
> > Thanks
>|||How many bytes on disk a value of that datatype requires.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
> what is Storage bytes? Thanks
> "SQL Menace" wrote:
>> Those are storage bytes, lookup decimal and numeric in BOL
>> Precision Storage bytes
>> 1 - 9 5
>> 10-19 9
>> 20-28 13
>> 29-38 17
>> Denis the SQL Menace
>> http://sqlservercode.blogspot.com/
>>
>> Jen wrote:
>> > Hi,
>> >
>> > I am kind of confused regarding the numeric data type. If I define a numeric
>> > column to be precision 38 (the maximum allowed), in EM the column length
>> > automatically turns to be 17. What's the relationship between 38 and 17?
>> > Thanks
>>|||Is it true that if the length is 17, then the minimum digits wil be 29 and
the maximum digits will be 38? Thanks
"Tibor Karaszi" wrote:
> How many bytes on disk a value of that datatype requires.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Jen" <Jen@.discussions.microsoft.com> wrote in message
> news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
> > what is Storage bytes? Thanks
> >
> > "SQL Menace" wrote:
> >
> >> Those are storage bytes, lookup decimal and numeric in BOL
> >>
> >> Precision Storage bytes
> >> 1 - 9 5
> >> 10-19 9
> >> 20-28 13
> >> 29-38 17
> >>
> >> Denis the SQL Menace
> >> http://sqlservercode.blogspot.com/
> >>
> >>
> >>
> >> Jen wrote:
> >> > Hi,
> >> >
> >> > I am kind of confused regarding the numeric data type. If I define a numeric
> >> > column to be precision 38 (the maximum allowed), in EM the column length
> >> > automatically turns to be 17. What's the relationship between 38 and 17?
> >> > Thanks
> >>
> >>
>|||No, that is now how it work. Compare to int vs. smallint, for instance. You would be saying that an
int cannot hold numbers lower than some 32767? That is not the case. A larger storage area can hold
a larger *span* of values. We are not really talking about digits, we are talking about the values
that these digits represent. The values can be represented as binary, 10 base decimal or
hexadecimal, us humans tend to prefer a 10 base decimal representation.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:622544A1-7A46-434A-A539-82D3C5052A52@.microsoft.com...
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:
>> How many bytes on disk a value of that datatype requires.
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>>
>> "Jen" <Jen@.discussions.microsoft.com> wrote in message
>> news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
>> > what is Storage bytes? Thanks
>> >
>> > "SQL Menace" wrote:
>> >
>> >> Those are storage bytes, lookup decimal and numeric in BOL
>> >>
>> >> Precision Storage bytes
>> >> 1 - 9 5
>> >> 10-19 9
>> >> 20-28 13
>> >> 29-38 17
>> >>
>> >> Denis the SQL Menace
>> >> http://sqlservercode.blogspot.com/
>> >>
>> >>
>> >>
>> >> Jen wrote:
>> >> > Hi,
>> >> >
>> >> > I am kind of confused regarding the numeric data type. If I define a numeric
>> >> > column to be precision 38 (the maximum allowed), in EM the column length
>> >> > automatically turns to be 17. What's the relationship between 38 and 17?
>> >> > Thanks
>> >>
>> >>
>>|||Jen,
If you define a column as decimal/numeric with size 29, then each value
will use 17 bytes of disk space. A column defined as decimal(29,0) can
hold the values -99999999999999999999999999999 thru
99999999999999999999999999999.
If you define the column as decimal(30,0), you can store even bigger
numbers (one decimal digit more). However, each value will still use
(only) 17 bytes of disk space. The same is true for a decimal(38,0).
If you define the column as decimal(28,0), you can 'only' store numbers
between -9999999999999999999999999999 and 9999999999999999999999999999.
However, for the values of this column only 13 bytes of disk space are
needed per value.
HTH,
Gert-Jan
Jen wrote:
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:
> > How many bytes on disk a value of that datatype requires.
> >
> > --
> > Tibor Karaszi, SQL Server MVP
> > http://www.karaszi.com/sqlserver/default.asp
> > http://www.solidqualitylearning.com/
> >
> >
> > "Jen" <Jen@.discussions.microsoft.com> wrote in message
> > news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
> > > what is Storage bytes? Thanks
> > >
> > > "SQL Menace" wrote:
> > >
> > >> Those are storage bytes, lookup decimal and numeric in BOL
> > >>
> > >> Precision Storage bytes
> > >> 1 - 9 5
> > >> 10-19 9
> > >> 20-28 13
> > >> 29-38 17
> > >>
> > >> Denis the SQL Menace
> > >> http://sqlservercode.blogspot.com/
> > >>
> > >>
> > >>
> > >> Jen wrote:
> > >> > Hi,
> > >> >
> > >> > I am kind of confused regarding the numeric data type. If I define a numeric
> > >> > column to be precision 38 (the maximum allowed), in EM the column length
> > >> > automatically turns to be 17. What's the relationship between 38 and 17?
> > >> > Thanks
> > >>
> > >>
> >
> >

question about numeric type

Hi,
I am kind of confused regarding the numeric data type. If I define a numeric
column to be precision 38 (the maximum allowed), in EM the column length
automatically turns to be 17. What's the relationship between 38 and 17?
Thanks
Those are storage bytes, lookup decimal and numeric in BOL
Precision Storage bytes
1 - 9 5
10-19 9
20-28 13
29-38 17
Denis the SQL Menace
http://sqlservercode.blogspot.com/
Jen wrote:
> Hi,
> I am kind of confused regarding the numeric data type. If I define a numeric
> column to be precision 38 (the maximum allowed), in EM the column length
> automatically turns to be 17. What's the relationship between 38 and 17?
> Thanks
|||what is Storage bytes? Thanks
"SQL Menace" wrote:

> Those are storage bytes, lookup decimal and numeric in BOL
> Precision Storage bytes
> 1 - 9 5
> 10-19 9
> 20-28 13
> 29-38 17
> Denis the SQL Menace
> http://sqlservercode.blogspot.com/
>
> Jen wrote:
>
|||How many bytes on disk a value of that datatype requires.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...[vbcol=seagreen]
> what is Storage bytes? Thanks
> "SQL Menace" wrote:
|||Is it true that if the length is 17, then the minimum digits wil be 29 and
the maximum digits will be 38? Thanks
"Tibor Karaszi" wrote:

> How many bytes on disk a value of that datatype requires.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "Jen" <Jen@.discussions.microsoft.com> wrote in message
> news:E236F084-6DDA-46E6-9AC2-447ED7E1E364@.microsoft.com...
>
|||No, that is now how it work. Compare to int vs. smallint, for instance. You would be saying that an
int cannot hold numbers lower than some 32767? That is not the case. A larger storage area can hold
a larger *span* of values. We are not really talking about digits, we are talking about the values
that these digits represent. The values can be represented as binary, 10 base decimal or
hexadecimal, us humans tend to prefer a 10 base decimal representation.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jen" <Jen@.discussions.microsoft.com> wrote in message
news:622544A1-7A46-434A-A539-82D3C5052A52@.microsoft.com...[vbcol=seagreen]
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:
|||Jen,
If you define a column as decimal/numeric with size 29, then each value
will use 17 bytes of disk space. A column defined as decimal(29,0) can
hold the values -99999999999999999999999999999 thru
99999999999999999999999999999.
If you define the column as decimal(30,0), you can store even bigger
numbers (one decimal digit more). However, each value will still use
(only) 17 bytes of disk space. The same is true for a decimal(38,0).
If you define the column as decimal(28,0), you can 'only' store numbers
between -9999999999999999999999999999 and 9999999999999999999999999999.
However, for the values of this column only 13 bytes of disk space are
needed per value.
HTH,
Gert-Jan
Jen wrote:[vbcol=seagreen]
> Is it true that if the length is 17, then the minimum digits wil be 29 and
> the maximum digits will be 38? Thanks
> "Tibor Karaszi" wrote:

Wednesday, March 21, 2012

question about inserting varchar info (lenght limits?)

question about inserting varchar info (lenght limits?)
Assume I have a table column that accepts a user_id (the column is say varch
ar 15)
What happens if I send in a variable of length 16?
Is the entered string truncated and accepted by the database? (Assuming no c
onstraints or anything were programmed)Depends on you setting for ANSI_WARNINGS. If on, you get an error message.
This is easy to test:
CREATE TABLE #a(c1 varchar(5))
SET ANSI_WARNINGS OFF
INSERT INTO #a(c1) VALUES('fit')
INSERT INTO #a(c1) VALUES('Doesn''t fit')
SELECT * FROM #a
TRUNCATE TABLE #a
SET ANSI_WARNINGS ON
INSERT INTO #a(c1) VALUES('fit')
INSERT INTO #a(c1) VALUES('Doesn''t fit')
SELECT * FROM #a
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=...ublic.sqlserver
"david_in_Groton" <anonymous@.discussions.microsoft.com> wrote in message
news:416C3CA9-64CC-4C55-823B-B4A758139F2E@.microsoft.com...
quote:

> question about inserting varchar info (lenght limits?)
> Assume I have a table column that accepts a user_id (the column is say

varchar 15)
quote:

> What happens if I send in a variable of length 16?
> Is the entered string truncated and accepted by the database? (Assuming no

constraints or anything were programmed)

question about inserting varchar info (lenght limits?)

question about inserting varchar info (lenght limits?)
Assume I have a table column that accepts a user_id (the column is say varchar 15)
What happens if I send in a variable of length 16?
Is the entered string truncated and accepted by the database? (Assuming no constraints or anything were programmed)Depends on you setting for ANSI_WARNINGS. If on, you get an error message.
This is easy to test:
CREATE TABLE #a(c1 varchar(5))
SET ANSI_WARNINGS OFF
INSERT INTO #a(c1) VALUES('fit')
INSERT INTO #a(c1) VALUES('Doesn''t fit')
SELECT * FROM #a
TRUNCATE TABLE #a
SET ANSI_WARNINGS ON
INSERT INTO #a(c1) VALUES('fit')
INSERT INTO #a(c1) VALUES('Doesn''t fit')
SELECT * FROM #a
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"david_in_Groton" <anonymous@.discussions.microsoft.com> wrote in message
news:416C3CA9-64CC-4C55-823B-B4A758139F2E@.microsoft.com...
> question about inserting varchar info (lenght limits?)
> Assume I have a table column that accepts a user_id (the column is say
varchar 15)
> What happens if I send in a variable of length 16?
> Is the entered string truncated and accepted by the database? (Assuming no
constraints or anything were programmed)sql

Tuesday, March 20, 2012

question about how to access column/field information

Hi,
I am just starting out in SQL Server and, as always, I am throwing myself in
at the deep end by trying more advanced things. I understand the basics like
INSERT, DELETE, UPDATE, SELECT etc What I am trying to do is pass some kind
of SQL object to a function which will then extract the field names, field
types, and field sizes from that object.
I have tried using DataSet and DataTable but I cannot see how I can extract
all three things I need from the column information. I'd also rather do what
I am doing on the actual table structure itself, rather than on a returned
set of records from the table (dataset).
Any help would be appreciated.
Thanks in advance,
RichardAs far as you are writing in the SQL Server group I think that you want to
get this information via TSQL ?!
If so, just look at this Query where #Orders can be replaced with the Name
of your Object to be queried. The Best way woud be to solve the Object name
to a ObjectID and to search for then, just for easy understanding it is
filtered via the object name here. :-)
Select sc.name,st.name,st.length from syscolumns sc
Inner join sysobjects so on
sc.id = so.id
Inner join systypes st on
sc.xtype = st.xtype
Where so.Xtype = 'U' And
so.name like 'Orders'
That will give you all the information youll wanted. If you want the
information via C# oder ADO.NET in common you could get i with FILLSCHEMA:
http://msdn.microsoft.com/library/d...r />
atopic.asp
HTH, Jens Smeyer.
"Richard" <acorn@.acorn.net> schrieb im Newsbeitrag
news:425cb509$1_3@.news.melbourne.pipenetworks.com...
> Hi,
> I am just starting out in SQL Server and, as always, I am throwing myself
> in at the deep end by trying more advanced things. I understand the basics
> like INSERT, DELETE, UPDATE, SELECT etc What I am trying to do is pass
> some kind of SQL object to a function which will then extract the field
> names, field types, and field sizes from that object.
> I have tried using DataSet and DataTable but I cannot see how I can
> extract all three things I need from the column information. I'd also
> rather do what I am doing on the actual table structure itself, rather
> than on a returned set of records from the table (dataset).
> Any help would be appreciated.
> Thanks in advance,
> Richard
>|||Richard wrote:
> Hi,
> I am just starting out in SQL Server and, as always, I am throwing
> myself in at the deep end by trying more advanced things. I
> understand the basics like INSERT, DELETE, UPDATE, SELECT etc What I
> am trying to do is pass some kind of SQL object to a function which
> will then extract the field names, field types, and field sizes from
> that object.
> I have tried using DataSet and DataTable but I cannot see how I can
> extract all three things I need from the column information. I'd also
> rather do what I am doing on the actual table structure itself,
> rather than on a returned set of records from the table (dataset).
> Any help would be appreciated.
> Thanks in advance,
> Richard
You can interrogate the system tables or ideally use the
INFORMATION_SCHEMA views. See BOL for more information.
I'm not sure what you mean by "pass a SQL object to a function", unless
what you're saying is that you want a stored procedure that returns some
meta data for a table.
If you're executing a query and want the result set details, you can do
that easily from client in ADO or ADO.Net.
Can you provide some additional detail about what you're trying to do?
David Gugick
Imceda Software
www.imceda.com|||What I'd like to be able to do is, given the name of a table or a table
object of some description, interrogate that table for all the fields in
that table, and also information about those fields, so that I can process
that information.
As much information as possible about the fields would be nice, but at the
minimum I need the field name, type (char, bigint etc) and size of the
field.
Thanks again,
Richard
"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:%23UAyWB$PFHA.2972@.TK2MSFTNGP14.phx.gbl...
> Richard wrote:
> You can interrogate the system tables or ideally use the
> INFORMATION_SCHEMA views. See BOL for more information.
> I'm not sure what you mean by "pass a SQL object to a function", unless
> what you're saying is that you want a stored procedure that returns some
> meta data for a table.
> If you're executing a query and want the result set details, you can do
> that easily from client in ADO or ADO.Net.
> Can you provide some additional detail about what you're trying to do?
>
> --
> David Gugick
> Imceda Software
> www.imceda.com|||Richard
Read up this article
http://www.sommarskog.se/dynamic_sql.html
"Richard" <acorn@.acorn.net> wrote in message
news:425cc012_1@.news.melbourne.pipenetworks.com...
> What I'd like to be able to do is, given the name of a table or a table
> object of some description, interrogate that table for all the fields in
> that table, and also information about those fields, so that I can process
> that information.
> As much information as possible about the fields would be nice, but at the
> minimum I need the field name, type (char, bigint etc) and size of the
> field.
> Thanks again,
> Richard
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:%23UAyWB$PFHA.2972@.TK2MSFTNGP14.phx.gbl...
>|||Use the INFORMATION_SCHEMA views as suggested.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Richard" <acorn@.acorn.net> wrote in message news:425cc012_1@.news.melbourne.pipenetworks.co
m...
> What I'd like to be able to do is, given the name of a table or a table ob
ject of some
> description, interrogate that table for all the fields in that table, and
also information about
> those fields, so that I can process that information.
> As much information as possible about the fields would be nice, but at the
minimum I need the
> field name, type (char, bigint etc) and size of the field.
> Thanks again,
> Richard
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:%23UAyWB$PFHA.2972@.TK2MSFTNGP14.phx.gbl...
>

Question about getting data from multiple tables

I have a report that receives a Soc. Sec. No. parameter. The first table I
need data from is keyed on SSN. This table also has Id. column.
I need to use the value in the ID column to retreive data from a different
data table for another part of the report.
From a design point of view how should I approach this.
--
DonThis will most likely work, depending on the nature of your report:
Simply join your tables in your source query (data tab). You can group your
layout based on data in the first table. Or you can base different parts of
the report on different source queries, but make both queries filtered on
the same data.
--
'(' Jeff A. Stucker
\
Business Intelligence
www.criadvantage.com
---
"Don" <Don@.discussions.microsoft.com> wrote in message
news:A2E283F5-EFDA-487A-B03E-D5469582871A@.microsoft.com...
>I have a report that receives a Soc. Sec. No. parameter. The first table I
> need data from is keyed on SSN. This table also has Id. column.
> I need to use the value in the ID column to retreive data from a different
> data table for another part of the report.
> From a design point of view how should I approach this.
> --
> Don

Monday, March 12, 2012

Question about Excel source - how to modify columns?

Hi,

I have a package that uses an Excel file source. There appears to be no place to modify the column data types as you can with a flat file manager. As such, the source columns do not match the columns in the database.

I believe I must be overlooking something here.

Can someone please tell me how I can modify the Excel column datatypes?

Thanks

nevermind

found it under "advanced editor", "input and output" properties

|||

Well, I'm having a difficult time changing the datatypes on my excel columns.

The "input and output properties" allow you to change the datatypes of the "output columns", but if I try to modify the "external columns", I get an error that the source column doesn't match or something to that effect. If I just try to change the length, it gives me other errors. So it seems I can't really change these?

So I left all the "external" columns as unicode strings with the original length, and changed the "output columns" to regular strings and the floats to numerics.

But in the end, this has solved nothing because I'm still getting truncation errors. So obviously changing the "output columns" isn't enough.

I really don't think I'm doing this right! Need help.

|||Sadie,
Use a derived column or a data conversion component to change your data types. Let the Excel manager and Excel Source do their jobs. The output data type is dictated by the driver and the data in the Excel sheet.|||You need to use a derived column or data conversion transform to change the data types. Excel data is stored as unicode, and it is a conversion step to change it to ANSI. Same thing for the floats.|||

Ok, duh

makes sense... just never worked with an excel spreadsheet before

Friday, March 9, 2012

question about column that will link 2 tables.

I realize this a Transact SQL forum. If there is another forum more appropriate, please let me know.

I want to think about the best way to begin to set up this database with respect to the column that will link these tables together.

The info below is what has been given to me as my client's list of values/info she would like to include.

Here is my first pass, knowing that the ID columns need to be fixed. Thank you!

http://www.hazzsoftwaresolutions.net/db_diag.htm

Patients

ID

First Name

Last Name

DOB

1234-12

Joe

Smith

3/1/1960

5432-30

Bob

Jones

1/3/1991

3232-22

Paul

White

5/12/1982

PatientVisits

AutoID

PatientID

VisitDate

WeightPounds

HeightInches

1

1234-12

10/11/2001

180

68.5

2

1234-12

2/1/2003

185

68.7

3

5432-30

4/5/2000

155

63.0

4

5432-30

11/6/2000

165

63.0

5

5432-30

5/12/2001

164

63.5

6

3232-22

1/17/2002

220

75.0

The primary key of PatientVisits table can be set (PatientID,VisitDate) because i think a pacient have a single visit on a day; you can renounce at AutoID that is redundant.The link between the 2 tables is : Patients(ID) <>PatientVisits(PatientID)|||

Thank you Gigi.

Yes, I guess the PatientID is set by an administrator rather than by the sytem. There would be no automatically generated new Patient created by 'the database system.; I don't know how the hyphen '-' would work.

Good point about the visits per day. I would think that in some settings, there might be more than one visit per day by patients...so it looks like it would need have and autoincremented identity property.

Greg

Wednesday, March 7, 2012

Question about BCP

I have a data file that I am trying to BCP IN into a table. The table has an IDENTITY column as PK.

If the BCP process fails in between (there would be about 500,000 records in the data file) and I restart the process, would the records be overwritten into the table or deleted and re-isnerted if the record already exists? I noticed that it does not create duplicates. So either its over writing the existing records or ignoring them and inserting the new records. I did not find any documenttion regarding this in BOL.

Thanks.

I did a test, found that BCP always tries to append data from file to table. You can use-bbatch_size to specifies the number of rows per batch of data copied. Each batch is copied to the server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for every batch. By default, all data in the specified data file is copied in one batch.

And there is a -E switch, which decides whether using IDENTITY values from file, or generate a new unique IDENTITY. So when you use BCP with -E option to import data from file that may bring duplicate key values, an error will be raised saying vilation primary key constraint. You can take a look at this SQL SDK article:

http://msdn.microsoft.com/library/en-us/coprompt/cp_bcp_61et.asp?frame=true

|||

I do use the -E option. I have a stored proc that I run from multiple Query Analyzer windows parallely to transfer the data faster.

Here's how the command builds up:

SET @.str = 'bcp ' + @.db + '.dbo.' + @.table + ' in "' + @.Fileloc + '" -f"C:\mount\backup21\DocPhrase.fmt" -S' + @.server + ' -T -E '

When I run multiple processes, occassionally one of them fails with locking issue. So I re-run the stored proc. I noticed it doesnt complain about existing records. And there are no duplicates too if I run the bcp in multiple times. So I was wondering whether BCP ignores if the record already exists in the table or overwrites it.

|||

ndinakar:

When I run multiple processes, occassionally one of them fails with locking issue. So I re-run the stored proc. I noticed it doesnt complain about existing records. And there are no duplicates too if I run the bcp in multiple times. So I was wondering whether BCP ignores if the record already exists in the table or overwrites it.

Really strange, in my testing BCP always tried to append rows, not ignore, nor overwritel; and if run same bcp multiple times with -E option, the duplicates vilation error will be raised. I use Profiler to trace SQL server, and found actually BCP calls 'insert bulk' command, not update.

Are you sure your identify column is primary key of the table? How about add a hint to the bcp command as:

-h CHECK_CONSTRAINT

Or you canget the Profiler trace to see what happens why the BCP runs to your SQL server.

|||

Yes you are right. It does throw an error if I try to BCP in the same file again if the BCP in was successful the first time.

I believe what was happening in mi situation was, since the entire bcp in is treated as one transaction, if for some locking reason it fails none of the records are inserted. That is why when I re run the process, it does not complain and nicely inserts the records. The records were never in in the first place.

|||It should be as you say, something related to sql transaction in BCP. And there is a-bbatch_size option that can be used to specifies the number of rows per batch of data copied. Each batch is copied to the server as one transaction. SQL Server commits or rolls back, in the case of failure, the transaction for every batch.

Saturday, February 25, 2012

Question about "WITH (ROWLOCK)" hint.

(SQL Server 2000, SP3a)
Hello all!
I've got a trigger on a table that I want to cause a column in another table
to be
updated:
update t
set t.[DateLastUpdated] = getdate()
from [dbo].[MyOtherTable] as t with (rowlock)
inner join inserted as i with (nolock) on i.[PK] = t.[PK]
Is the WITH (ROWLOCK) hint any different if I express it this way:
update t with (rowlock)
set t.[DateLastUpdated] = getdate()
from [dbo].[MyOtherTable] as t
inner join inserted as i with (nolock) on i.[PK] = t.[PK]
My guess is that it isn't (since the compiler will warn you if there are inc
ompatible lock
types between the "alias" and the table) -- so I just thought it'd be the sa
me. But, I'm
not really sure and was hoping for clarification.
Thanks for any help you can provide!
John PetersonJohn,
I don't believe so but your example is a bad one<g>. First off you don't
need to issue a NOLOCK on the Inserted table since the session doing the
updating is the only one that has access to it. I don't know if it even
does anything to tell you the truth. And the ROWLOCK should not be needed
either. Your joining on a PK and unless your updating most of the table it
should use rowlocks anyway. Hints should only be used where absolutely
necessary. You need to prove to yourself they are needed before using them
otherwise you risk getting poor performance overall.
Andrew J. Kelly
SQL Server MVP
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:utDiYXb8DHA.488@.TK2MSFTNGP12.phx.gbl...
> (SQL Server 2000, SP3a)
> Hello all!
> I've got a trigger on a table that I want to cause a column in another
table to be
> updated:
> update t
> set t.[DateLastUpdated] = getdate()
> from [dbo].[MyOtherTable] as t with (rowlock)
> inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> Is the WITH (ROWLOCK) hint any different if I express it this way:
> update t with (rowlock)
> set t.[DateLastUpdated] = getdate()
> from [dbo].[MyOtherTable] as t
> inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> My guess is that it isn't (since the compiler will warn you if there are
incompatible lock
> types between the "alias" and the table) -- so I just thought it'd be the
same. But, I'm
> not really sure and was hoping for clarification.
> Thanks for any help you can provide!
> John Peterson
>|||> from [dbo].[MyOtherTable] as t with (rowlock)
What do you think you're gaining here?
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Hello Andrew!
Thanks -- yeah, I figured my NOLOCK hint was likely superfluous. The table
that being
updated is a fairly high-use table, and explicitly providing a ROWLOCK hint
has been
helpful in the past. But, your words of caution are appreciated!
Thanks for your help!
John Peterson
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:OYCc4xb8DHA.1948@.TK2MSFTNGP12.phx.gbl...
> John,
> I don't believe so but your example is a bad one<g>. First off you don't
> need to issue a NOLOCK on the Inserted table since the session doing the
> updating is the only one that has access to it. I don't know if it even
> does anything to tell you the truth. And the ROWLOCK should not be needed
> either. Your joining on a PK and unless your updating most of the table i
t
> should use rowlocks anyway. Hints should only be used where absolutely
> necessary. You need to prove to yourself they are needed before using the
m
> otherwise you risk getting poor performance overall.
> --
> Andrew J. Kelly
> SQL Server MVP
>
> "John Peterson" <j0hnp@.comcast.net> wrote in message
> news:utDiYXb8DHA.488@.TK2MSFTNGP12.phx.gbl...
> table to be
> incompatible lock
> same. But, I'm
>|||"Aaron Bertrand - MVP" <aaron@.TRASHaspfaq.com> wrote in message
news:%23wEOE0b8DHA.452@.TK2MSFTNGP11.phx.gbl...
> What do you think you're gaining here?
I had hoped to mitigate the possibility of conversion deadlocks with this ap
proach
(deadlocks that are caused due to row-->page lock escalation). I was just c
urious to
learn whether specifying the hint at one or the other locations might have a
ny difference.

> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>

Question about "WITH (ROWLOCK)" hint.

(SQL Server 2000, SP3a)
Hello all!
I've got a trigger on a table that I want to cause a column in another table to be
updated:
update t
set t.[DateLastUpdated] = getdate()
from [dbo].[MyOtherTable] as t with (rowlock)
inner join inserted as i with (nolock) on i.[PK] = t.[PK]
Is the WITH (ROWLOCK) hint any different if I express it this way:
update t with (rowlock)
set t.[DateLastUpdated] = getdate()
from [dbo].[MyOtherTable] as t
inner join inserted as i with (nolock) on i.[PK] = t.[PK]
My guess is that it isn't (since the compiler will warn you if there are incompatible lock
types between the "alias" and the table) -- so I just thought it'd be the same. But, I'm
not really sure and was hoping for clarification.
Thanks for any help you can provide!
John PetersonJohn,
I don't believe so but your example is a bad one<g>. First off you don't
need to issue a NOLOCK on the Inserted table since the session doing the
updating is the only one that has access to it. I don't know if it even
does anything to tell you the truth. And the ROWLOCK should not be needed
either. Your joining on a PK and unless your updating most of the table it
should use rowlocks anyway. Hints should only be used where absolutely
necessary. You need to prove to yourself they are needed before using them
otherwise you risk getting poor performance overall.
--
Andrew J. Kelly
SQL Server MVP
"John Peterson" <j0hnp@.comcast.net> wrote in message
news:utDiYXb8DHA.488@.TK2MSFTNGP12.phx.gbl...
> (SQL Server 2000, SP3a)
> Hello all!
> I've got a trigger on a table that I want to cause a column in another
table to be
> updated:
> update t
> set t.[DateLastUpdated] = getdate()
> from [dbo].[MyOtherTable] as t with (rowlock)
> inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> Is the WITH (ROWLOCK) hint any different if I express it this way:
> update t with (rowlock)
> set t.[DateLastUpdated] = getdate()
> from [dbo].[MyOtherTable] as t
> inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> My guess is that it isn't (since the compiler will warn you if there are
incompatible lock
> types between the "alias" and the table) -- so I just thought it'd be the
same. But, I'm
> not really sure and was hoping for clarification.
> Thanks for any help you can provide!
> John Peterson
>|||> from [dbo].[MyOtherTable] as t with (rowlock)
What do you think you're gaining here?
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/|||Hello Andrew!
Thanks -- yeah, I figured my NOLOCK hint was likely superfluous. The table that being
updated is a fairly high-use table, and explicitly providing a ROWLOCK hint has been
helpful in the past. But, your words of caution are appreciated!
Thanks for your help!
John Peterson
"Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
news:OYCc4xb8DHA.1948@.TK2MSFTNGP12.phx.gbl...
> John,
> I don't believe so but your example is a bad one<g>. First off you don't
> need to issue a NOLOCK on the Inserted table since the session doing the
> updating is the only one that has access to it. I don't know if it even
> does anything to tell you the truth. And the ROWLOCK should not be needed
> either. Your joining on a PK and unless your updating most of the table it
> should use rowlocks anyway. Hints should only be used where absolutely
> necessary. You need to prove to yourself they are needed before using them
> otherwise you risk getting poor performance overall.
> --
> Andrew J. Kelly
> SQL Server MVP
>
> "John Peterson" <j0hnp@.comcast.net> wrote in message
> news:utDiYXb8DHA.488@.TK2MSFTNGP12.phx.gbl...
> > (SQL Server 2000, SP3a)
> >
> > Hello all!
> >
> > I've got a trigger on a table that I want to cause a column in another
> table to be
> > updated:
> >
> > update t
> > set t.[DateLastUpdated] = getdate()
> > from [dbo].[MyOtherTable] as t with (rowlock)
> > inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> >
> > Is the WITH (ROWLOCK) hint any different if I express it this way:
> >
> > update t with (rowlock)
> > set t.[DateLastUpdated] = getdate()
> > from [dbo].[MyOtherTable] as t
> > inner join inserted as i with (nolock) on i.[PK] = t.[PK]
> >
> > My guess is that it isn't (since the compiler will warn you if there are
> incompatible lock
> > types between the "alias" and the table) -- so I just thought it'd be the
> same. But, I'm
> > not really sure and was hoping for clarification.
> >
> > Thanks for any help you can provide!
> >
> > John Peterson
> >
> >
>|||"Aaron Bertrand - MVP" <aaron@.TRASHaspfaq.com> wrote in message
news:%23wEOE0b8DHA.452@.TK2MSFTNGP11.phx.gbl...
> > from [dbo].[MyOtherTable] as t with (rowlock)
> What do you think you're gaining here?
I had hoped to mitigate the possibility of conversion deadlocks with this approach
(deadlocks that are caused due to row-->page lock escalation). I was just curious to
learn whether specifying the hint at one or the other locations might have any difference.
> --
> Aaron Bertrand
> SQL Server MVP
> http://www.aspfaq.com/
>

Monday, February 20, 2012

Question

I have a fromdate column and a todate column in a table. I want the todate column to be one year later then the fromdate. I have the dates for the fromdate column, is there an easy way to set the values in the todate column?I like a triggers very much - you can use them in your case.|||If you are not planning on updating the todate at all, you can use a computed column:

create table test1
(fromdate datetime not null,
todate as dateadd (yy, 1, fromdate))

insert into test1 (fromdate)
values (getdate())

select *
from test1|||If you are not planning on ever updating the todate (or really even if you were), I don't know why you would ever even store it. If you needed it you could always just calculate it.|||rhigdon: Good point. Especially with this easy of a calculation.|||THere are times where it saves time to store calcs, like on some databases I have stored the business days difference between two dates within the table to save time, but for something as simple as this...|||Originally posted by adama777
I have a fromdate column and a todate column in a table. I want the todate column to be one year later then the fromdate. I have the dates for the fromdate column, is there an easy way to set the values in the todate column?

You can use the following statement:

update Table_X
set todate = fromdate + 365

--Suresh.|||If you are going to use triggers, use the DateAdd function instead. Leap years don't have 365 days.

blindman|||Yes. Yes. I forgot about the leap years. You can simply use:

You can use the following statement:

update Table_X
set todate = dateadd(yyyy, 1, fromdate)

--Suresh.